Creating loop in Pandas DataFrame with conditional value in cell

后端 未结 1 628
隐瞒了意图╮
隐瞒了意图╮ 2021-01-24 05:05

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\',          


        
相关标签:
1条回答
  • 2021-01-24 05:33

    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
    
    0 讨论(0)
提交回复
热议问题