Skip to content Skip to sidebar Skip to footer

Pandas Dataframe String Formatting (access A Given Column)

I try to use new-style formatting to display the entry at a given/specified column: np.random.seed(1234) df = pd.DataFrame(np.random.randint(7, size=(2, 2)), columns=['a', 'b']) c

Solution 1:

I think you can remove '' in one['a']:

print("Here is {one[a]} and {two}".format(one=c, two=c['b']))
Here is3and6

Solution 2:

You can use loc to get the value of column a.

print("Here is {one} and {two}".format(one=c.loc['a'], two=c['b']))
Here is3and6

You can also do it this way.

df['sum'] = df.sum(axis=1)

n = 0# Get the first row.    >>> "{row[a]} and {row[b]} makes {row[sum]}".format(row=df.iloc[n, :])
'3 and 6 makes 9'

Post a Comment for "Pandas Dataframe String Formatting (access A Given Column)"