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.
Is there any way to achieve this?
CodePudding user response:
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)