python - Dataframe Boolean Logic Index Match -
i have created pandas data frame , filter data based on boolean logic. i'd closer excels' index match function simple filtering. have researched lot of other threads.
when apply filter, data frame returns 0 true values. why 0 true values being returned when have been flexible logic? and;
if introduced 5th column, column
'd'
,random.randomint(100-1000,100)
, logic use conditionally find maximum values columnd
? i.e. can force data frame return highest true values column, in event multiple true values returned?
advice appreciated. thank in advance.
import pandas pd df = pd.dataframe({ 'step': [1,1,1,1,1,1,2,2,2,2,2,2], 'a': [4,5,6,7,4,5,6,7,4,5,6,7], 'b': [10,20,30,40,10,20,30,40,10,20,30,40], 'c': [0,0.5,1,1.5,2,2.5,0,0.5,1,1.5,2.0,2.5] }) columns = ['step','a','b','c'] df=df[columns] new_df=df[(df.step == 1) & (df.a == 4|5|6|7) & (df.b == 10|20|30|40)] new_df
using dataframe.query() method:
in [7]: new_df = df.query("step==1 , in [4,5,6,7] , b in [10,20,30,40]") in [8]: new_df out[8]: step b c 0 1 4 10 0.0 1 1 5 20 0.5 2 1 6 30 1.0 3 1 7 40 1.5 4 1 4 10 2.0 5 1 5 20 2.5
Comments
Post a Comment