在 Pandas 数据框的一列中将所有 NaN 值替换为零
使用单行DataFrame.fillna()和DataFrame.replace()方法可以轻松地替换数据框中的 NaN 或 null 值。我们将讨论这些方法以及一个演示如何使用它的示例。
DataFrame.fillna():
此方法用于用特定值填充 null 或 null 值。
Syntax: DataFrame.fillna(self, value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)
Parameters: This method will take following parameters:
- value (scalar, dict, Series, or DataFrame): Specify the value to use to fill null values.
- method([‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None], default None): Specify the method used to fill null values.
- axis (0 or ‘index’, 1 or ‘columns’): Specify the axis along which to fill missing values.
- inplace(bool, default False): If bool value is True, fill in-place which will modify any other views on this object.
- limit(int, default None): Specify the maximum number of consecutive NaN values to forward/backward fill.
- downcast(dict, default is None): A dict of item dtype of what to downcast if possible, or the string ‘infer’ which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible).
Returns: DataFrame or None. Object with null values filled or None if inplace=True.
代码:创建一个数据框。
Python3
# Import Pandas Library
import pandas as pd
# Import Numpy Library
import numpy as np
# Create a DataFrame
df = pd.DataFrame([[np.nan, 2, 3, np.nan],
[3, 4, np.nan, 1],
[1, np.nan, np.nan, 5],
[np.nan, 3, np.nan, 4]])
# Show the DataFrame
print(df)
Python3
# Filling null values
# with 0
df.fillna(value = 0,
inplace = True)
# Show the DataFrame
print(df)
Python3
# Import Pandas Library
import pandas as pd
# Import Numpy Library
import numpy as np
# Create a DataFrame
df = pd.DataFrame([[np.nan, 2, 3, np.nan],
[3, 4, np.nan, 1],
[1, np.nan, np.nan, 5],
[np.nan, 3, np.nan, 4]])
# Show the DataFrame
print(df)
Python3
# Filling null values with 0
df = df.replace(np.nan, 0)
# Show the DataFrame
print(df)
输出:
代码:用零替换所有 NaN 值
Python3
# Filling null values
# with 0
df.fillna(value = 0,
inplace = True)
# Show the DataFrame
print(df)
输出:
DataFrame.replace():
此方法用于将空值或空值替换为特定值。
Syntax: DataFrame.replace(self, to_replace=None, value=None, inplace=False, limit=None, regex=False, method=’pad’)
Parameters: This method will take following parameters:
- to_replace(str, regex, list, dict, Series, int, float, None): Specify the values that will be replaced.
- value(scalar, dict, list, str, regex, default value is None): Specify the value to replace any values matching to_replace with.
- inplace(bool, default False): If a value is True, in place. Note: this will modify any other views on this object.
- limit(int, default None): Specify the maximum size gap to forward or backward fill.
- regex(bool or same types as to_replace, default False): If a value is True then to_replace must be a string. Alternatively, this could be a regular expression or a list, dict, or array of regular expressions in which case to_replace must be None.
- method {‘pad’, ‘ffill’, ‘bfill’, None}: Specify the method to use when for replacement, when to_replace is a scalar, list or tuple and value is None.
Returns: DataFrame. Object after replacement.
代码:创建一个数据框。
Python3
# Import Pandas Library
import pandas as pd
# Import Numpy Library
import numpy as np
# Create a DataFrame
df = pd.DataFrame([[np.nan, 2, 3, np.nan],
[3, 4, np.nan, 1],
[1, np.nan, np.nan, 5],
[np.nan, 3, np.nan, 4]])
# Show the DataFrame
print(df)
输出:
代码:用零替换所有 NaN 值
Python3
# Filling null values with 0
df = df.replace(np.nan, 0)
# Show the DataFrame
print(df)
输出: