Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only columns from a DataFrame based on column Values

Tags:

python

pandas

I'm studying pandas just know and I'm stuck in this problem.

I have this dataframe:

                      apple                    orange                    banana
0  (2021-01-01 00:00:00, 2)  (2021-01-02 00:00:00, 1)  (2021-01-01 00:00:00, 4)
1  (2021-01-02 00:00:00, 5)  (2021-01-04 00:00:00, 2)  (2021-01-03 00:00:00, 6)
2  (2021-01-03 00:00:00, 8)  (2021-01-06 00:00:00, 3)  (2021-01-04 00:00:00, 7)

How will I exclude a column by their value?

For example, I just want those fruits that have number > 6. Expected result

                      apple                    banana
0  (2021-01-01 00:00:00, 2)  (2021-01-01 00:00:00, 4)
1  (2021-01-02 00:00:00, 5)  (2021-01-03 00:00:00, 6)
2  (2021-01-03 00:00:00, 8)  (2021-01-04 00:00:00, 7)

Other example, fruits with >=2 and <=3 Expected result

                      apple                    orange
0  (2021-01-01 00:00:00, 2)  (2021-01-02 00:00:00, 1)
1  (2021-01-02 00:00:00, 5)  (2021-01-04 00:00:00, 2)
2  (2021-01-03 00:00:00, 8)  (2021-01-06 00:00:00, 3)

Is this kind of used cases possible using pandas? Thanks for any help.

like image 794
manilaT Avatar asked Jun 26 '26 08:06

manilaT


1 Answers

With your shown samples, please try following with Boolean masking + loc function pf Pandas:

m = df.apply(lambda s:s.str[1]).gt(6).any()
df.loc[:,m]

Explanation: For selecting 2nd element of tuple value of each column using lambda function, once its selected then checking whichever element is having more than 6 value, then passing it to any function which will give True if any element is greater than 6 is found in whole column.

Output will be as follows:

                      apple                    banana
0  (2021-01-01 00:00:00, 2)  (2021-01-01 00:00:00, 4)
1  (2021-01-02 00:00:00, 5)  (2021-01-03 00:00:00, 6)
2  (2021-01-03 00:00:00, 8)  (2021-01-04 00:00:00, 7)
like image 182
RavinderSingh13 Avatar answered Jun 27 '26 22:06

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!