Python - 删除 DataFrame 中缺失的 (NaN) 值
要删除缺失值,即 NaN 值,请使用该dropna()方法。首先,让我们导入所需的库 -
import pandas as pd
读取 CSV 并创建一个 DataFrame -
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv")
使用dropna()删除缺失值。NaN 将在dropna()使用后显示缺失值-
dataFrame.dropna()
示例
以下是完整代码
import pandas as pd输出结果# 读取csv文件
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\CarRecords.csv")
print("DataFrame with some NaN (missing) values...\n",dataFrame)
# 计算 DataFrame 中的行和列
print("\nNumber of rows and column in our DataFrame = ",dataFrame.shape)
# 删除缺失值
print("\nDataFrame after removing NaN values...\n",dataFrame.dropna())
这将产生以下输出 -
DataFrame with some NaN (missing) values...Car Place UnitsSold
0 Audi Bangalore 80.0
1 Porsche Mumbai NaN
2 RollsRoyce Pune 100.0
3 BMW Delhi NaN
4 Mercedes Hyderabad 80.0
5 Lamborghini Chandigarh 80.0
6 Audi Mumbai NaN
7 Mercedes Pune 120.0
8 Lamborghini Delhi 100.0
Number of rows and colums in our DataFrame = (9, 3)
DataFrame after removing NaN values ...
Car Place UnitsSold
0 Audi Bangalore 80.0
2 RollsRoyce Pune 100.0
4 Mercedes Hyderabad 80.0
5 Lamborghini Chandigarh 80.0
7 Mercedes Pune 120.0
8 Lamborghini Delhi 100.0
以上是 Python - 删除 DataFrame 中缺失的 (NaN) 值 的全部内容, 来源链接: utcz.com/z/361498.html