📅  最后修改于: 2023-12-03 15:38:57.839000             🧑  作者: Mango
在 Pandas 中,NaN 表示缺失或未知数据。这些 NaN 值可能会使数据集中的计算和分析产生错误结果。因此,通常需要从 Pandas 列中删除 NaN 值。
以下是在 Pandas 中删除 NaN 值的两种方法:
使用 Pandas 的 dropna()
函数,可以轻松删除包含 NaN 值的行或列。
示例代码:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, 2, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': [9, 10, 11, 12]})
print("DataFrame before dropping NaN values:")
print(df)
# Drop NaN values in columns
df.dropna(axis=1, inplace=True)
print("\nDataFrame after dropping NaN values in columns:")
print(df)
# Drop NaN values in rows
df.dropna(inplace=True)
print("\nDataFrame after dropping NaN values in rows:")
print(df)
输出结果:
DataFrame before dropping NaN values:
A B C
0 1.0 5.0 9
1 2.0 NaN 10
2 NaN NaN 11
3 4.0 8.0 12
DataFrame after dropping NaN values in columns:
C
0 9
1 10
2 11
3 12
DataFrame after dropping NaN values in rows:
A B C
0 1.0 5.0 9
3 4.0 8.0 12
使用 Pandas 的 fillna()
函数,可以将 NaN 值替换为某个值,例如 0 或平均值。
示例代码:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, 2, np.nan, 4],
'B': [5, np.nan, np.nan, 8],
'C': [9, 10, 11, 12]})
print("DataFrame before replacing NaN values:")
print(df)
# Replace NaN values with 0
df.fillna(value=0, inplace=True)
print("\nDataFrame after replacing NaN values:")
print(df)
# Replace NaN values with mean
df['B'].fillna(value=df['B'].mean(), inplace=True)
print("\nDataFrame after replacing NaN values with mean:")
print(df)
输出结果:
DataFrame before replacing NaN values:
A B C
0 1.0 5.0 9
1 2.0 NaN 10
2 NaN NaN 11
3 4.0 8.0 12
DataFrame after replacing NaN values:
A B C
0 1.0 5.0 9
1 2.0 0.0 10
2 0.0 0.0 11
3 4.0 8.0 12
DataFrame after replacing NaN values with mean:
A B C
0 1.0 5.0 9
1 2.0 6.5 10
2 NaN 6.5 11
3 4.0 8.0 12
以上就是在 Pandas 列中删除 NaN 值的两种常用方法。可以根据实际情况选择适合自己的方法。