Get first column in dataframe that exists from list of column names

Viewed 61

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?

3 Answers

You can do it with:

col = next(filter(lambda c: c in df, cols), None)

One idea:

col = next(iter(df.columns.intersection(cols, sort=False)), None)

@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
Related