简体   繁体   中英

Perform operation on DataFrame and get result as Dataframe which matches either of the condition present in another DataFrame in pandas

I have a DataFrame df1:

df1 = pd.DataFrame([[31,12,32,65],[42,19,22,78],[76,13,45,68],[43,37,43,71],[43,18,34,73]],columns=['A', 'B', 'C', 'D'])

and another DataFrame df2:

df2 = pd.DataFrame([["B",">20"],["C",">40"],["D","<70"],["A","<40"]],columns = ["Feature","Condition"])

Want to perform operation on df1 using respective conditions present in df2. The result should be a DataFrame which meets either of the condition present in df2 as shown below Output.

Output = pd.DataFrame([[31,12,32,65],[76,13,45,68],[43,37,43,71]],columns=['A', 'B', 'C', 'D']) 

How to do it? Please help.

You can use agg to create condition then query :

# create condition 
conds = " or ".join(df2.agg(" ".join, axis=1).tolist())

# apply condition
df = df1.query(conds)

print(df)

    A   B   C   D
0  31  12  32  65
2  76  13  45  68
3  43  37  43  71

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM