Tie_24
Tie_24

Reputation: 647

Column conditions in dataframe groups

       A     B     C     D
0    Red  10.0  11.5  12.0
1    Red  10.5  11.5  12.0
2    Red  11.0  11.5  12.0
3    Red  12.0  11.5  12.0
4  White  10.0  10.5  11.0
5  White  10.5  10.5  11.0
6  White  11.0  10.5  11.0
7  White  12.0  10.5  11.0

I want to create a new df1, with next conditions:

Per each A group ("Red" and "White"):

I mean, the new df1 should be:

       A     B     C     D
3    Red  12.0  11.5  12.0
5  White  10.5  10.5  11.0

Upvotes: 2

Views: 27

Answers (1)

jezrael
jezrael

Reputation: 862471

You can use boolean indexing with concat, then drop_duplicates and for default ordering add sort_index:

m1 = df['B'] == df['C']
m2 = df['B'] == df['D']
df = pd.concat([df[m1], df[m2]]).drop_duplicates('A').sort_index()
print (df)
       A     B     C     D
3    Red  12.0  11.5  12.0
5  White  10.5  10.5  11.0

Upvotes: 4

Related Questions