# 如何根据熊猫另一列中的条件生成具有值的新列

Sanjana Jha

``````        Tel    MC             WT

AAA    Rubber         9999
BBB    Tree           0
CCC    Rub            12
AAA    Other          20
BBB    Same           999
DDD    Other-Same     70
``````

``````          df.loc[(df[WT] == 0 | df[WT] == 999 | df[WT] == 9999 | df[WT] == 99999),'Comment'] = 'Fail'
``````

``````         AttributeError: 'str' object has no attribute 'loc'
``````

``````       Tel    MC             WT      Comment
AAA    Rubber         9999    Fail
BBB    Tree           0       Fail
CCC    Rub            12
AAA    Other          20
BBB    Same           999     Fail
DDD    Other-Same     70
``````

``````df.loc[df['WT'].isin([0, 999,9999,99999]),'Comment'] = 'Fail'
print (df)
Tel          MC    WT Comment
0  AAA      Rubber  9999    Fail
1  BBB        Tree     0    Fail
2  CCC         Rub    12     NaN
3  AAA       Other    20     NaN
4  BBB        Same   999    Fail
5  DDD  Other-Same    70     NaN
``````

``````df['Comment'] = np.where(df['WT'].isin([0, 999,9999,99999]), 'Fail', '')
print (df)
Tel          MC    WT Comment
0  AAA      Rubber  9999    Fail
1  BBB        Tree     0    Fail
2  CCC         Rub    12
3  AAA       Other    20
4  BBB        Same   999    Fail
5  DDD  Other-Same    70
``````

0 条评论