1

I have a dataframe that has either column A or B

I was hoping for a syntax that uses the or operator to have a stntax like:

Value = df.at['Total','A'] or df.at['Total','B']

but I still receive a KeyError exception. Is there a shorthand way to achieve this instead of doing something like:

if 'A' in df.columns:
    Value = df.at['Total','A']
else:
    Value = df.at['Total','B']

3 Answers 3

2

I would use ternary conditional operator:

df.at["Total", "B" if "B" in df else "A"]
Sign up to request clarification or add additional context in comments.

Comments

1

Since you have an inconsistent format, why not make it consistent? rename your columns to have a unique name:

df.rename(columns={'B': 'A'}).at['Total', 'A']

This is handy to generalize to multiple aliases:

aliases = ['B', 'C', 'D']
df.rename(columns=dict.fromkeys(aliases, 'A')).at['Total', 'A']

further generalization

Alternatively, using an index intersection, which also enables you to pass an arbitrary number of aliases:

df.at['Total', next(iter(df.columns.intersection(['A', 'B'])))]

Note that in case of multiple matches (e.g if you have both A and B) this gives you the first match in order of the DataFrame's columns. If you want the first match in a custom order:

# first try to match B, then A
aliases = ['B', 'A']
df.at['Total', next(iter(pd.Index(aliases).intersection(df.columns)))]

Comments

0

If your pandas dataframe will always have the same number of columns in the same order, you could use the column index instead of the name. For example, to grab the whole first column:

df[[1]]

Or to grab the column entry in the first row:

df.iloc[0, 1]

1 Comment

The number of columns will be different so unfortunately this will not work. I have to go based on column name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.