Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove a row in pandas using Chain rule

I have below code

import pandas as pd
df = pd.DataFrame({"A":[12, 4, 5, None, 1],
                   "B":[7, 2, 54, 3, None],
                   "C":[20, 16, 11, 3, 8],
                   "D":[14, 3, None, 2, 6]})
df
df[df['A'].notna()]

Last line remove entire row of df for which A is None.

However to improve readability, I was achieve this final dataframe is one line where I created df, using chain rule.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any way to achieve this?

>Solution :

Use loc with a function/lambda:

df = (pd.DataFrame({"A":[12, 4, 5, None, 1],
                    "B":[7, 2, 54, 3, None],
                    "C":[20, 16, 11, 3, 8],
                    "D":[14, 3, None, 2, 6]})
        .loc[lambda d: d['A'].notna()]
     )

output:

      A     B   C     D
0  12.0   7.0  20  14.0
1   4.0   2.0  16   3.0
2   5.0  54.0  11   NaN
4   1.0   NaN   8   6.0

Documentation:

Allowed inputs are:

[…]

A callable function with one argument (the calling Series or
DataFrame) and that returns valid output for indexing (one of the
above)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading