简体   繁体   English

Pandas 下降 null 值 - AttributeError:模块'pandas'没有属性'dropna'

[英]Pandas drop null values - AttributeError: module 'pandas' has no attribute 'dropna'

For a current project, I am trying to exclude all null values from a numeric table.对于当前项目,我试图从数值表中排除所有 null 值。

When applying the dropna() command to "drop" all values not including a number, I am getting the following message: AttributeError: module 'pandas' has no attribute 'dropna' .当应用dropna()命令“删除”所有不包括数字的值时,我收到以下消息: AttributeError: module 'pandas' has no attribute 'dropna'

Is there any smart tweak to get this running?有什么聪明的调整来让它运行吗? The corresponding code looks like this:相应的代码如下所示:

df['Rating_Recommend'] = pd.dropna(df['Rating_Recommend'])
df['Rating_Recommend'] = pd.to_numeric(df['Rating_Recommend'])

rating_recommend = df.Rating_Recommend.mean()
print(rating_recommend)

Can you try this?你能试试这个吗? df['Rating_Recommend'].dropna(inplace=True)

There is no requirement to use pd object to call the dropna() function. You can directly use it on the data frame.不需要使用pd object来调用dropna() function,直接在data frame上使用即可。

df['Rating_Recommend'] = df['Rating_Recommend'].dropna()

or 

df['Rating_Recommend'].dropna(inplace=True)

Check the docs for more options.查看文档以获取更多选项。

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html

dropna() is a DataFrame/Series method, not a module's function. So you need to do something like this: dropna()是一个 DataFrame/Series 方法,而不是模块的 function。所以你需要做这样的事情:

df['Rating_Recommend'].dropna()

But then, it's pointless to do但是,这样做毫无意义

df['Rating_Recommend'] = df['Rating_Recommend'].dropna()

In stead, from your code, you should do this:相反,从您的代码中,您应该这样做:

df['Rating_Recommend'] = pd.to_numeric(df['Rating_Recommend'], errors='coerce')

rating_mean = df['Rating_Recommend'].mean()

It should be either:它应该是:
df['Rating_Recommend'] = df['Rating_Recommend'].dropna() or df['Rating_Recommend'] = df['Rating_Recommend'].dropna()
df['Rating_Recommend'].dropna(inplace=True) because dropna is a method of pd.DataFrame and pd.Series . df['Rating_Recommend'].dropna(inplace=True)因为dropnapd.DataFramepd.Series的一种方法。 In your case, df['Rating_Recommend'] is a pandas Series.在您的情况下, df['Rating_Recommend']是一个 pandas 系列。

There are plenty of options to use dropna(), I recommend you check it out!!有很多选项可以使用 dropna(),我建议你检查一下!!

# Here are all parameters for dropna(). U won't be needing any to just get rid     
#of "Nan" and null values, but they do have useful functionalities  
DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)

This should accomplish what your asking:这应该完成你的要求:

df['Rating_Recommend'].dropna(inplace=True)

I hope you check the Doc, you will find it really helpful.我希望你检查文档,你会发现它真的很有帮助。 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM