【问题标题】:Python : get access to a column of a dataframe with multiindexPython:使用多索引访问数据框的列
【发布时间】:2020-12-12 11:17:53
【问题描述】:

假设我有这个带有多索引的数据框:

                                           Montant
IBAN                  Date       Balance
FR3724687328623865    2020-09-16 654.75      -2.00
                      2020-09-17 23.65      -88.00
                      2020-09-21 1537.00   2700.20
                      2020-09-25 8346.20   -163.21
                      2020-09-28 6247.60   -468.90
...                                            ...
FR8723498262347632    2020-10-06 13684.11  2708.00
FR9687234782365235    2020-10-16 4353.42   6311.00
                      2020-10-28 9641.23    562.78
                      2020-11-30 5436.95    -45.12
                      2020-09-30 4535.34    -43.56

我们如何访问“余额”或“日期”列中的数据,我不明白为什么这不起作用:

bal = df["Montant"]["Balance"]

bal = df.loc[("Montant", "Balance")]

【问题讨论】:

  • 你读过docs吗?它可以为您的问题提供答案
  • 这能回答你的问题吗? pandas, how to access multiIndex dataframe?
  • 是的,我已阅读,但我尝试在“Montant”上得到一个 keyError,这就是为什么我不知道它是如何工作的

标签: python pandas dataframe multi-index


【解决方案1】:

你应该使用Index.get_level_values:

In [505]: df
Out[505]: 
                                        Montant
IBAN               Date       Balance          
FR3724687328623865 2020-09-16  654.75      -2.0
2020-09-17         23.65      -88.00        NaN
2020-09-21         1537.00     2700.20      NaN
2020-09-25         8346.20    -163.21       NaN
2020-09-28         6247.60    -468.90       NaN

你可以通过labels

In [509]: df.index.get_level_values('Date')
Out[509]: Index(['2020-09-16', '23.65', '1537.00', '8346.20', '6247.60'], dtype='object', name='Date')

In [510]: df.index.get_level_values('Balance')
Out[510]: Float64Index([654.75, -88.0, 2700.2, -163.21, -468.9], dtype='float64', name='Balance')

或:

通过indices:

In [512]: df.index.get_level_values(1)
Out[512]: Index(['2020-09-16', '23.65', '1537.00', '8346.20', '6247.60'], dtype='object', name='Date')

In [513]: df.index.get_level_values(2)
Out[513]: Float64Index([654.75, -88.0, 2700.2, -163.21, -468.9], dtype='float64', name='Balance')

【讨论】:

    【解决方案2】:

    如果您需要索引,请转到doc,如果您需要索引作为普通列;做

    df = df.reset_index()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-18
      • 2013-05-20
      • 1970-01-01
      • 2018-02-07
      • 2019-05-27
      • 2016-09-28
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多