简体   繁体   English

我应该怎么做才能解决我在 python 中使用 dropna 和 fillna 的问题?

[英]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 值。

你在这里看到 NaN 值

When i write this code to drop the rows which have NaN values;当我编写此代码以删除具有 NaN 值的行时;

它在使用 dropna 之后

When i write this code to add "empty" which have NaN values;当我编写此代码以添加具有 NaN 值的“空”时;

而这一个使用fillna

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.

相关问题 我应该如何处理python中的不可下标问题? - What I should to do with not subscriptable problem in python? Dropna 没有下降,fillna 没有填充,我的列表理解无法理解如何摆脱 nans (python) - Dropna isn't dropping, fillna isn't filling and my list comprehension can't comprehend how to get rid of nans (python) 当我运行我的程序时,显示了像 cannot import name 'Chat' from 'telegram.ext' 这样的消息。 我应该怎么做才能解决这个问题? - The message like cannot import name 'Chat' from 'telegram.ext' was shown while I was running my program. What should I do to solve this problem? 我如何解决这个 python 大学问题? - how do i solve this python college problem? 如何在 Python 中使用“for”来解决这个问题? - How do I solve this problem using 'for' in Python? 使用 Technical-analysis-library-in-python 时如何解决“AttributeError: 'Series' object has no attribute '_check_fillna'”错误 - How do I solve the "AttributeError: 'Series' object has no attribute '_check_fillna'" error when using the technical-analysis-library-in-python 我应该如何在按钮上运行我的python脚本 - What should I do on the button to run my python script 我该怎么做才能在Python代码中修复NoSuchElementException错误? - What should i do to fix the NoSuchElementException error in my python code? 我应该如何使用“np.around”方法解决我的问题? - How should I solve my problem with “np.around” method? 我搞砸了Python环境该怎么办? - What should I do with my messed up Python environment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM