pandas AttributeError: 'Series' 对象没有属性 'notna'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47769508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'Series' object has no attribute 'notna'
提问by Aman Singh
i have a csv file with multiple columns containing empty strings. Upon reading the csv into pandas dataframe, the empty strings get converted to NaN.
我有一个多列包含空字符串的 csv 文件。将 csv 读入 Pandas 数据帧后,空字符串将转换为NaN。
Now i want to append a string tag-
to the strings already present in the columns but to only those that have some values in it and not on those with NaN
现在我想将一个字符串附加tag-
到列中已经存在的字符串,但只附加到那些有一些值的字符串而不是那些带有NaN的字符串
this is what i was trying to do:
这就是我想要做的:
with open('file1.csv','r') as file:
for chunk in pd.read_csv(file,chunksize=1000, header=0, names=['A','B','C','D'])
if len(chunk) >=1:
if chunk['A'].notna:
chunk['A'] = "tag-"+chunk['A'].astype(str)
if chunk['B'].notna:
chunk['B'] = "tag-"+chunk['B'].astype(str)
if chunk['C'].notna:
chunk['C'] = "tag-"+chunk['C'].astype(str)
if chunk['D'].notna:
chunk['D'] = "tag-"+chunk['D'].astype(str)
and this is the error I'm getting:
这是我得到的错误:
AttributeError: 'Series' object has no attribute 'notna'
the final output that i want should be something like this:
我想要的最终输出应该是这样的:
A,B,C,D
tag-a,tab-b,tag-c,
tag-a,tag-b,,
tag-a,,,
,,tag-c,
,,,tag-d
,tag-b,,tag-d
回答by jezrael
I believe you need mask
for add tag-
to all columns together:
我相信您需要mask
一起添加tag-
到所有列:
for chunk in pd.read_csv('file1.csv',chunksize=2, header=0, names=['A','B','C','D']):
if len(chunk) >=1:
m1 = chunk.notna()
chunk = chunk.mask(m1, "tag-" + chunk.astype(str))
You need upgrade to last version of pandas, 0.21.0
.
您需要升级到最新版本的Pandas,0.21.0
.
You can check docs:
您可以查看文档:
In order to promote more consistency among the pandas API, we have added additional top-level functions
isna()
andnotna()
that are aliases forisnull()
andnotnull()
. The naming scheme is now more consistent with methods like.dropna()
and.fillna()
. Furthermore in all cases where .isnull() and .notnull() methods are defined, these have additional methods named.isna()
and.notna()
, these are included for classes Categorical, Index, Series, and DataFrame. (GH15001).The configuration option pd.options.mode.use_inf_as_null is deprecated, and pd.options.mode.use_inf_as_na is added as a replacement.
为了促进大PandasAPI之间的一致性,我们增加了额外的顶级功能
isna()
和notna()
是为别名isnull()
和notnull()
。命名方案现在类似的方法更加一致.dropna()
和.fillna()
。此外,在定义 .isnull() 和 .notnull() 方法的所有情况下,这些方法都有名为.isna()
and 的附加方法.notna()
,这些方法包含在 Categorical、Index、Series 和 DataFrame 类中。(GH15001)。不推荐使用配置选项 pd.options.mode.use_inf_as_null,并添加 pd.options.mode.use_inf_as_na 作为替代。