【发布时间】:2018-05-25 22:54:59
【问题描述】:
我有一个 csv 文件,其中包含多个包含空字符串的列。将 csv 读入 pandas 数据帧后,空字符串将转换为 NaN。
现在我想将一个字符串tag- 附加到列中已经存在的字符串,但只附加到那些具有某些值的字符串,而不是那些具有 NaN
这就是我想要做的:
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)
这是我得到的错误:
AttributeError: 'Series' object has no attribute 'notna'
我想要的最终输出应该是这样的:
A,B,C,D
tag-a,tab-b,tag-c,
tag-a,tag-b,,
tag-a,,,
,,tag-c,
,,,tag-d
,tag-b,,tag-d
【问题讨论】:
-
notna是一个函数。称它为notna()。 -
看来错字了:(
-
@jezrael 实际上,您的回答很好。如果 OP 使用正确的版本,
notna将返回一个函数,其真实性将被评估为 true。这实际上是一个 XY 问题。 -
仍然没有改善我在提到它时仍然遇到同样的错误
notna()@COLDSPEED -
@GabbarSingh 就用
notnull()
标签: python python-3.x pandas dataframe