Creating loop in Pandas DataFrame with conditional value in cell

匿名 (未验证) 提交于 2019-12-03 02:36:02

问题:

In the script below, I assign the values 0 or 1 if the DataFrame cell has No or Yes in them.

answer= {'account': ['Adam', 'Ben', 'Tom', 'Isabel'],      'a1': ['No', 'Yes', 'Yes', 'No'],      'a2': ['No', 'Yes', 'No', 'No'],      'a3': ['No', 'Yes', 'No', 'No'],      'a4': ['Yes', 'No', 'Yes', 'Yes']} RPI = pd.DataFrame.from_dict(answer) 

I'm trying to create a loop or a function for the RPI.loc statements below so I don't have to repeat the 50 columns that I have. Is there a way to do that?

RPI.loc[RPI['a1'] == 'No', 'a1'] = 0 RPI.loc[RPI['a1'] == 'Yes', 'a1'] = 1 RPI.loc[RPI['a2'] == 'No', 'a2'] = 0 RPI.loc[RPI['a2'] == 'Yes', 'a2'] = 1 RPI.loc[RPI['a3'] == 'No', 'a3'] = 0 RPI.loc[RPI['a3'] == 'Yes', 'a3'] = 1 RPI.loc[RPI['a4'] == 'No', 'a4'] = 0 RPI.loc[RPI['a4'] == 'Yes', 'a4'] = 1      a1  a2  a3  a4  account 0   0   0   0   1   Adam 1   1   1   1   0   Ben 2   1   0   0   1   Tom 3   0   0   0   1   Isabel 

回答1:

Need replace by dict:

RPI = RPI.replace({'No':0, 'Yes':1}) print (RPI)    a1  a2  a3  a4 account 0   0   0   0   1    Adam 1   1   1   1   0     Ben 2   1   0   0   1     Tom 3   0   0   0   1  Isabel 

If need specify columns for replace by positions add iloc:

print (RPI.iloc[:, 0:4])     a1   a2   a3   a4 0   No   No   No  Yes 1  Yes  Yes  Yes   No 2  Yes   No   No  Yes 3   No   No   No  Yes  RPI.iloc[:, 0:4] = RPI.iloc[:, 0:4].replace({'No':0, 'Yes':1}) print (RPI)   a1 a2 a3 a4 account 0  0  0  0  1    Adam 1  1  1  1  0     Ben 2  1  0  0  1     Tom 3  0  0  0  1  Isabel 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!