如何通过检查条件来替换dataFrame中的值?

时间:2016-05-24 18:44:16

标签: python pandas

我有以下数据框:

A  |   Date1  | Date2 
10 | 2/2/2016 | 3/2/2016
11 | 1/5/2016 | 1/5/2016
12 | 2/3/2016 | 2/3/2016
13 | 1/5/2016 | 3/2/2013

如果 Date1 中的值等于 Date2 ,我想将 A 列中的值设为 0 。 最终结果:

A  |   Date1  | Date2 
10 | 2/2/2016 | 3/1/2016
0  | 1/5/2016 | 1/5/2016
0  | 2/3/2016 | 2/3/2016
13 | 1/5/2016 | 3/2/2013

我想在不编写for循环的情况下这样做。我可以使用申请吗?

你可以重新创建我的df:

df = pd.DataFrame([[10, "2/2/2016", "3/2/2016" ] , [11, "1/5/2016", "1/5/2016"] , [12 , "2/3/2016" , "2/3/2016" ] , [13, "1/5/2016", "3/2/2013"]])
df.columns = ['A','B','C']

2 个答案:

答案 0 :(得分:3)

使用mask

import pandas as pd


df = pd.DataFrame([[10, "2/2/2016", "3/2/2016" ] ,
                   [11, "1/5/2016", "1/5/2016"] ,
                   [12 , "2/3/2016" , "2/3/2016" ] ,
                   [13, "1/5/2016", "3/2/2013"]])
df.columns = ['A','B','C']
print (df)
    A         B         C
0  10  2/2/2016  3/2/2016
1  11  1/5/2016  1/5/2016
2  12  2/3/2016  2/3/2016
3  13  1/5/2016  3/2/2013

df['A'] = df.mask(df.B == df.C, 0)
print (df)
    A         B         C
0  10  2/2/2016  3/2/2016
1   0  1/5/2016  1/5/2016
2   0  2/3/2016  2/3/2016
3  13  1/5/2016  3/2/2013

where的解决方案:

df['A'] = df.where(df.B != df.C, 0)
print (df)
    A         B         C
0  10  2/2/2016  3/2/2016
1   0  1/5/2016  1/5/2016
2   0  2/3/2016  2/3/2016
3  13  1/5/2016  3/2/2013

答案 1 :(得分:1)

使用jezrael的设置

设置

import pandas as pd


df = pd.DataFrame([[10, "2/2/2016", "3/2/2016" ] ,
                   [11, "1/5/2016", "1/5/2016"] ,
                   [12 , "2/3/2016" , "2/3/2016" ] ,
                   [13, "1/5/2016", "3/2/2013"]])
df.columns = ['A','B','C']

使用loc

df.loc[df.B == df.C, 'A'] = 0

print df

    A         B         C
0  10  2/2/2016  3/2/2016
1   0  1/5/2016  1/5/2016
2   0  2/3/2016  2/3/2016
3  13  1/5/2016  3/2/2013