【问题标题】:Is it possible to explicitly to refer to indexes by their name in pandas data frame?是否可以在 pandas 数据框中通过名称显式引用索引?
【发布时间】:2013-06-02 06:46:08
【问题描述】:

在 pandas 中,DataFrames 允许组合列来构建索引(如果每一行都有来自这些列的值的唯一组合)。这样做的好处之一是简化了语法,而不是:

my_data_frame[(my_data_frame['column_name_1']==0) & (my_data_frame['column_name_2']==1)]

我们可以使用:

my_data_frame[(0,1)]

以下是我如何使用多个列来构建索引的示例:

import pandas as pd

ls = [{'col1':10, 'col2':0, 'col3':0, 'col4':100}, {'col1':20, 'col2':0, 'col3':1, 'col4':200}, {'col1':30, 'col2':1, 'col3':0, 'col4':300}, {'col1':40, 'col2':1, 'col3':1, 'col4':400}]    
df = pd.DataFrame(ls).set_index(['col2','col3'])

df.ix[(0,0)]['col1']  # returns 10
df.ix[('col3'=1, 'col2'=0)] # <----- This does not work. (SyntaxError: invalid syntax)

是否可以执行上述代码最后一行中给出的操作?我当然可以:

df[(1, 2, 0, 'aaa', 10)]

但为此我总是需要记住索引的顺序。如果我能做这样的事情会更好:

df[(age=10, scale=2, grade=0, name='aaa', size=1)]

【问题讨论】:

  • 我认为答案是否定的......但你可以编写一个辅助函数来做到这一点。
  • Pandas 对文件的改进?我发现你正在寻找的糖很有用,希望它能以某种方式内置

标签: python indexing dataframe pandas


【解决方案1】:

您可以编写自己的助手:

In [11]: df1
Out[11]:
           col1  col4
col2 col3
0    0       10   100
     1       20   200
1    0       30   300
     1       40   400

In [12]: d = {'col3': 1, 'col2': 0}

如果你确定你传递了所有的名字,你可以把它们按正确的顺序排列:

In [13]: t = tuple(map(d.get, df1.index.names))

In [14]: t
Out[14]: (0, 1)

In [15]: df1.loc[t]
Out[15]:
col1     20
col4    200
Name: (0, 1), dtype: int64

如果你不这样做,并且想要更健壮的东西,你可以做一些更棘手的事情,如下所示(当然有一种更有效的方法来做到这一点,而不会减少)。但这里有一个想法:

def reduce_kv(df, kv):
    try:
        return df.xs(kv[1], level=kv[0])
    except (AttributeError,):
        if df.index.name == kv[0]:
            return df.loc[kv[1]]
        else:
            raise AttributeError("Level %s not found" % kv[0])

In [17]: reduce(reduce_kv, d.items(), df1)
Out[17]:
col1     20
col4    200
Name: 1, dtype: int64

注意:名称可能也需要更改...

【讨论】:

    猜你喜欢
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2015-12-10
    • 1970-01-01
    • 2013-05-09
    • 2022-06-24
    • 2020-10-22
    相关资源
    最近更新 更多