Drop a few lines of pandas data using lambda

I am currently having a problem with a chain of methods when manipulating data frames in pandas, here is the structure of my data:

import pandas as pd

lst1 = range(100)
lst2 = range(100)
lst3 = range(100)
df = pd.DataFrame(
    {'Frenquency': lst1,
     'lst2Tite': lst2,
     'lst3Tite': lst3
    })

the question is getting records (rows) if the frequency is less than 6, but this needs to be done in the chain of methods.

I know that using the traditional method is easy, I could just do

df[df["Frenquency"]<6]

to get an answer.

However, the question is how to do this with a chain of methods, I tried something like

df.drop(lambda x:x.index if x["Frequency"] <6 else null)

but he raised a mistake "[<function <lambda> at 0x7faf529d3510>] not contained in axis"

Can anyone highlight this issue?

+4
source share
3 answers

Will it satisfy your needs?

df.mask(df.Frequency >= 6).dropna()
0
source

Or maybe this:

df.drop(i for i in df.Frequency if i >= 6)

Or use inplace:

df.drop((i for i in df.Frequency if i >= 6), inplace=True)
0

- query:

>>> df.query('Frenquency < 6')
   Frenquency  lst2Tite  lst3Tite
0           0         0         0
1           1         1         1
2           2         2         2
3           3         3         3
4           4         4         4
5           5         5         5
>>>

- :

df.rename(<something>).query('Frenquency <6').assign(<something>)

:

>>> (df.rename(columns={'Frenquency':'F'})
...    .query('F < 6')
...    .assign(FF=lambda x: x.F**2))
   F  lst2Tite  lst3Tite  FF
0  0         0         0   0
1  1         1         1   1
2  2         2         2   4
3  3         3         3   9
4  4         4         4  16
5  5         5         5  25
0

Source: https://habr.com/ru/post/1689747/


All Articles