[英]What should I do to solve my problem with dropna and fillna in python?
I see there are some missing data, in my data.我看到我的数据中有一些缺失的数据。 there are NaN values in "Type 2" column.
“类型 2”列中有 NaN 值。
When i write this code to drop the rows which have NaN values;当我编写此代码以删除具有 NaN 值的行时;
When i write this code to add "empty" which have NaN values;当我编写此代码以添加具有 NaN 值的“空”时;
I restarted my jupyter notebook but its still the same.我重新启动了我的 jupyter 笔记本,但它仍然是一样的。
您可以使用:
data = data[data['Type 2'].notna()]
Try尝试
data.dropna(axis=1, inplace=True)
axis=1 means columns axis=0 means rows if any column contains NaN it will remove entire column similarly row axis=1 表示列 axis=0 表示行 如果任何列包含 NaN 它将删除整列类似行
When you do data['Type 2'].dropna(inplace=True)
, that's creating a new series (for your "Type 2" column) and dropping the nans from that new data structure.当您执行
data['Type 2'].dropna(inplace=True)
,这将创建一个新系列(为您的“Type 2”列)并从该新数据结构中删除 nans。 It doesn't change your original dataframe.它不会更改您的原始数据框。
If you want to dropna from the DataFrame, you need to do it directly on that object, not on the column object.如果要从 DataFrame 中删除,则需要直接在该对象上执行,而不是在列对象上执行。 The following drops nans directly on the DataFrame:
以下将 nans 直接放在 DataFrame 上:
data.dropna(subset=['Type 2'], inplace=True)
Note that here we're calling dropna
on data
directly.请注意,这里我们直接对
data
调用dropna
。 subset
is the argument that you can use to tell pandas which columns to use to determine which rows to drop. subset
是您可以用来告诉熊猫使用哪些列来确定要删除哪些行的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.