5

I would like to represent a row in a pandas DataFrame with a formatted string referencing the columns. The best method I have found is to cast the row to a dict and then string.format()

Assuming a pd.DataFrame df with the columns 'specimen' and 'date':

r = df.loc[0]
print("{specimen}_{date}".format(**r.to_dict()))

can be used to apply the format string using the column data.

This does not seem very efficient. Is there must be a better way to do this in pandas directly without converting to a dict while maintaining the flexibility of explicitly naming columns in the format string?

Update:

The ultimate purpose is to generate tick labels for a matplotlib plot. Using the answer below:

plot.set_yticklabels(["{specimen}_{date}".format(**r) for i,r in df.iterrows()])

1 Answer 1

7

You can just use the row itself. It is a Series which supports dict-like access to the items:

>>> d
   A     B      C
0  1  This    One
1  2    is    Two
2  3  crud  Three
>>> "{A} then {B} and also {C}".format(**d.iloc[0])
'1 then This and also One'
Sign up to request clarification or add additional context in comments.

Comments

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.