vendredi 8 mai 2015

How do you check a condition of several pandas DataFrame.Series element-wise and apply the result to a new column?

I have a pandas.Dataframe.

df = pandas.DataFrame([(11,12,13),(1,3,5),(1,1,2)], columns=['a','b','c'])

    a   b   c
0  11  12  13
1   1   3   5
2   3   1   2

I would like to create a fourth column called d which would tell me if all elements in a row are bigger than 10. It this example it would look like this.

    a   b   c      d
0  11  12  13   True
1   1   3   5  False
2   3   1   2  False

I tried this which gives me a TypeError.

x = df['a']
y = df['b']
z = df['c']
df['d'] = df.apply(lambda x,y,z: True if x > 10 and y > 10 and z > 10 else False) 

I have also tried this which gives me a ValueError.

df['d'] = True
df['e'] = df['d'].where(df['a'] > 10 and df['b'] > 10 and df['c'] > 10, other=False)

Aucun commentaire:

Enregistrer un commentaire