[问题] 请问如何修改读进来csv的字段资料

楼主: PTTOuO (五股毕书尽)   2017-09-30 18:44:43
如题
这是我的data frame
http://i.imgur.com/USqiil5.jpg
我想要把Death year字段资料为nan的变成0其他都变成1
import pandas as pd
mydata_csv = pd.read_csv('D:\Python\character-deaths.csv',sep = ',',encoding = 'utf-8')
mydata_csv
del mydata_csv['Book of Death']
del mydata_csv['Death Chapter']
if mydata_csv['Death Year'] == None:
mydata_csv['Death Year'] = 0
else:
mydata_csv['Death Year'] = 1
这个是我的程式码
到if那里就错误了QQ
作者: djshen (djshen)   2017-09-30 19:07:00
也不说有什么错误讯息
作者: johnny78615 (Johnny)   2017-09-30 19:26:00
import numpy as npif mydata_csv['Death Year'] == np.nan这样试试
楼主: PTTOuO (五股毕书尽)   2017-09-30 19:27:00
ValueError: The truth value of a Series is ambiguous.Use a.empty, a.bool(), a.item(), a.any() or a.all().我改用 mydata_csv['Death Year'] = np.where(mydata_csv['Death Year']==None, 1, 0) 可以修改里面的资料了..但是通通变成0了QQ
作者: Sunal (SSSSSSSSSSSSSSSSSSSSSSS)   2017-09-30 22:14:00
np.where(pd.isnull(df['Death Year']), 0, 1)
作者: fischcheng (布阿送)   2017-10-01 08:20:00
goo.gl/xtV2SSpython里面np.nan跟np.nan不相等。本来dataframe里的也不是真的np.nan。不然直接用fillna试试其他用where不等于1取代。打错,适用fillna填上0,然后其他用不等于0 填上1
作者: jiyu520 (不要鲫鱼我)   2017-10-01 16:08:00
先确定pd里面的资料格式然后用最小单元(一格)先取代看看是否成功

Links booklink

Contact Us: admin [ a t ] ucptt.com