I would like to turn NaN
values in the column GP
in the dataframe draft
(which has 22 rows) into 0
. I tried these lines but they don't work
import pandas as pd
draft = pd.read_csv('Draft year.csv')
if draft['GP'].isnull():
draft['GP'] = 0
In R there is ifelse
which does the job nicely but I dont know equivalent in python. Really appreciate any help
The .fillna function works for this, you can specify the 'draft' series first if u only want to fillna with 0 for that column. Example below:
import pandas as pd
print data3
data3 = pd.read_csv("data2.csv")
data3["Name"].fillna(0, inplace = True)
data3
Name Name2 Time
0 NaN NaN 1
1 NaN NaN 2
2 G G 3
Name Name2 Time
0 0 NaN 1
1 0 NaN 2
2 G G 3
您可以使用列表推导并将NaN
替换为零。
draft['GP'] = [0 if np.isnan(x) else x for x in draft.GP]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.