获取 Pandas DataFrame 中的数字单元格值

Get a numerical cell value in a Pandas DataFrame


import pandas as pd

Sem_limiar = pd.read_csv("C:\Users\Luiz Alegro\Downloads\BR1_Trend_IBOV_portfolio_holding_and_NAVs_10.csv", skiprows=5, skipfooter=16, names=['Date', 'NAV', 'Return', 'Interest', 'NAVvlty', 'FiltRet', 'NAV_SEM'], engine='python')
Com_limiar = pd.read_csv("C:\Users\Luiz Alegro\Downloads\BR1_Trend_IBOV2_portfolio_holding_and_NAVs_10.csv", skiprows=5, skipfooter=16, names=['Date', 'NAV', 'Return', 'Interest', 'NAVvlty', 'FiltRet_', 'NAV_COM'], engine='python')

Sem_limiar = Sem_limiar[['Date','NAV_SEM']]
Com_limiar = Com_limiar[['Date','NAV_COM']]

Sem_limiar['Date'] = pd.to_datetime(Sem_limiar['Date'])
Com_limiar['Date'] = pd.to_datetime(Com_limiar['Date'])

Resumo5 = Sem_limiar
Resumo5['NAV_COM'] = Com_limiar['NAV_COM']

Resumo5.set_index('Date', drop=True, inplace=True)

display(Resumo5.loc['2012-01-04','NAV_SEM'])

I can´t get the float value a specific cell in the pandas dataframe. What should I use? .loc?

就是这么简单:

float(df['column_name'][row_number])

其中:

  • df 是数据框
  • column_name是列名
  • row_number 是该行的整数。

函数 float() 转换为浮点数。

示例数据框:

   year  average_temperature
0  1820              -6.1100
1  1821              -5.5600
2  1822              -5.8500
3  1823              -6.3550
4  1824              -5.1754
5  1825              -5.1500

第3行平均温度的浮点数:

float(df['average_temperature'][3])