Home > Software design >  Selecting a single value from a dataframe
Selecting a single value from a dataframe

Time:06-05

How can I select a single value through pandas library and assign it to a variable

import pandas as pd
returns = [0.0322, 0.3211, 0.0032]
symbols = ['A', 'B', 'C']

retdf = pd.DataFrame(returns, index=symbols, columns=['ret'])
retdf = retdf.ret.nlargest(1)
print(retdf)

Outputs

B    0.3211

How can I select B?

CodePudding user response:

'B' is an index, and you can refer it like below

print(retdf.index[0])
  • Related