Janosh
Janosh

Reputation: 4732

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

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?

Upvotes: 1

Views: 296

Answers (3)

jezrael
jezrael

Reputation: 863291

One idea:

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

Upvotes: 3

alparslan mimaroğlu
alparslan mimaroğlu

Reputation: 1490

@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

Upvotes: 2

Learning is a mess
Learning is a mess

Reputation: 8277

You can do it with:

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

Upvotes: 3

Related Questions