python - How do you check a condition of several pandas DataFrame.Series element-wise and apply the result to a new column? -
i have pandas.dataframe.
df = pandas.dataframe([(11,12,13),(1,3,5),(1,1,2)], columns=['a','b','c']) b c 0 11 12 13 1 1 3 5 2 3 1 2
i create fourth column called d
tell me if each element in row bigger 10. example this.
b c d 0 11 12 13 true 1 1 3 5 false 2 3 1 2 false
i tried gives me typeerror.
x = df['a'] y = df['b'] z = df['c'] df['d'] = df.apply(lambda x,y,z: true if x > 10 , y > 10 , z > 10 else false)
i have tried gives me valueerror.
df['d'] = true df['e'] = df['d'].where(df['a'] > 10 , df['b'] > 10 , df['c'] > 10, other=false)
we can use np.all
on expression df > 10
:
in [423]: df['d'] = np.all(df > 10, axis=1) df out[423]: b c d 0 11 12 13 true 1 1 3 5 false 2 1 1 2 false in [421]: df > 10 out[421]: b c 0 true true true 1 false false false 2 false false false in [422]: np.all(df > 10, axis=1) out[422]: array([ true, false, false], dtype=bool)
Comments
Post a Comment