![](/img/trans.png)
[英]AttributeError: module 'pandas' has no attribute 'rolling'
[英]Pandas drop null values - AttributeError: module 'pandas' has no attribute 'dropna'
對於當前項目,我試圖從數值表中排除所有 null 值。
當應用dropna()
命令“刪除”所有不包括數字的值時,我收到以下消息: AttributeError: module 'pandas' has no attribute 'dropna'
。
有什么聰明的調整來讓它運行嗎? 相應的代碼如下所示:
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)
你能試試這個嗎? df['Rating_Recommend'].dropna(inplace=True)
不需要使用pd object來調用dropna() function,直接在data frame上使用即可。
df['Rating_Recommend'] = df['Rating_Recommend'].dropna()
or
df['Rating_Recommend'].dropna(inplace=True)
查看文檔以獲取更多選項。
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html
dropna()
是一個 DataFrame/Series 方法,而不是模塊的 function。所以你需要做這樣的事情:
df['Rating_Recommend'].dropna()
但是,這樣做毫無意義
df['Rating_Recommend'] = df['Rating_Recommend'].dropna()
相反,從您的代碼中,您應該這樣做:
df['Rating_Recommend'] = pd.to_numeric(df['Rating_Recommend'], errors='coerce')
rating_mean = df['Rating_Recommend'].mean()
它應該是:
df['Rating_Recommend'] = df['Rating_Recommend'].dropna()
或
df['Rating_Recommend'].dropna(inplace=True)
因為dropna
是pd.DataFrame
和pd.Series
的一種方法。 在您的情況下, df['Rating_Recommend']
是一個 pandas 系列。
有很多選項可以使用 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)
這應該完成你的要求:
df['Rating_Recommend'].dropna(inplace=True)
我希望你檢查文檔,你會發現它真的很有幫助。 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.