我有一个包含空字符串的多列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
我相信您需要^{} 将
tag-
添加到所有列中:你需要升级到熊猫的最新版本,
0.21.0
。您可以检查docs:
相关问题 更多 >
编程相关推荐