0 votes
1 view
ago in Data Science by (11.1k points)

I want to filter my df I have copied the small snippet of my output here including my desired result I want to filter by an index removing columns which do not conform to my search. I would like to only keep columns in which 5-day change is >= 10%.

I tried df = df.loc["5 Day Change" >= .1] but it didn't work and I don't know how to make it work.

               1    2   3   4

15/10/2020  23.53   15.06   396.700012  348.380005

16/10/2020  23.35   15.09   398.519989  348.399994

5 Day Change-0.049654 0.12  0.009   0.256

10 Day Change-0.014768 0.01 0.11    0.03

Return

                2   4

15/10/2020  15.06   348.380005

16/10/2020  15.09   348.399994

5 Day Change 0.12   0.256

10 Day Change 0.01  0.03

1 Answer

0 votes
ago by (22.5k points)

You can use .T to transpose your dataframe and then filter by row rather than the column as it is easier to filter by rows in pandas. Then, reverse it back:

In[1]: 

                       1      2           3           4

15/10/2020     23.530000  15.06  396.700012  348.380005

16/10/2020     23.350000  15.09  398.519989  348.399994

5 Day Change   -0.049654   0.12    0.009000    0.256000

10 Day Change  -0.014768   0.01    0.110000    0.030000

df = df.T

df = df[df['5 Day Change'] >= .1].T

df

Out[1]: 

                   2           4

15/10/2020     15.06  348.380005

16/10/2020     15.09  348.399994

5 Day Change    0.12    0.256000

10 Day Change   0.01    0.030000

 Improve your knowledge in data science from scratch using Data science online courses

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...