Given a list of column names, only some or none exist in a dataframe, what's the least verbose way of getting the first existing column or None
?
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"])
cols = ["d", "e", "c"]
This is fairly short but fails with StopIteration
for no matching columns:
col = next(filter(lambda c: c in df, cols))
df[col]
0 3
1 6
Name: c, dtype: int64
Is there a better way?
CodePudding user response:
You can do it with:
col = next(filter(lambda c: c in df, cols), None)
CodePudding user response:
One idea:
col = next(iter(df.columns.intersection(cols, sort=False)), None)
CodePudding user response:
@Learnings is a mess answered it beautifully and you should use that solution but here is another one line solution with walrus operator.
col = intersect[0] if (intersect:= [c for c in cols if c in df.columns]) else None