根据列值更改列值并检查其他列的NaN值

时间:2019-07-23 01:38:01

标签: python-3.x pandas dataframe

我有一个数据集,如图所示。如果“ statement”列包含单词“ America”,并且如果“ count_2”列的值不是NaN 那么我想在“ count_2”列中将该值设为NaN并对应于“ count_1”值1。

statement                                  count_1  count_2

The America is a large country             NaN      1
China is one of the large country in Asia  1        NaN
America has silicon valley                 1        Nan
America has a beautifil climate            1        NaN
Russia has a very cold temperature         NaN      1
Brazil has a tropical climate              NaN      1
America has the Google headquartter        1        NaN      

我尝试了以下代码

for i in range(len(statement)):
    if "america" in df['blobContent'][i].lower(): 
        df.loc[df['count_2'].notnull(), ['count_1', 'count_2']] = df.loc[df['count_2'].notnull(), ['count_2', 'count_1']].values

我希望得到如下结果:谢谢

statement                                  count_1  count_2

The America is a large country             1        NaN
China is one of the large country in Asia  1        NaN
America has silicon valley                 1        NaN
America has a beautifil climate            1        NaN
Russia has a very cold temperature         NaN      1
Brazil has a tropical climate              NaN      1
America has the Google headquartter        1        NaN

1 个答案:

答案 0 :(得分:0)

我们可以使用contains

而无需forloop
m=df.statement.str.contains('America')&df.count_2.notnull()
df.loc[m,['count_1', 'count_2']]=df.loc[m,['count_2', 'count_1']].values