简体   繁体   English

是否可以在 Pandas 数据框中设置多个索引?

[英]Is it possible to have multiple indexes into a pandas dataframe?

Is it possible to have multiple indexes at once for a single pandas DataFrame?是否有可能有多个索引一次为一个单一的数据框大熊猫?

By index I mean something close to what SQL databases or MongoDB would have to drastically increase the performance of some queries.通过索引,我的意思是类似于 SQL 数据库或 MongoDB 必须大幅提高某些查询的性能。

Imagine you have a dataframe of orders, and sometimes you would like to index into an order by its ID, and another time by indexing into an order by another key, say warehouse, client ID, time, something like that.想象一下,您有一个订单数据框,有时您想通过其 ID 索引到一个订单,而另一次通过另一个键索引到一个订单,例如仓库、客户 ID、时间等。

Short Answer : Yes.简短回答:是的。

Please refer https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html请参考https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html

s = pd.Series([1, 2, 3, 4])
df.set_index([s, s**2])
      month  year  sale
1 1       1  2012    55
2 4       4  2014    40
3 9       7  2013    84
4 16     10  2014    31

Another example for multiple index:多索引的另一个例子:

dct = {'id' : (0,0,1,1,2,2,3,3,4,4,5,5),
    'Store': ('A','A','A','A','A','A','B','B','B','C','C','C'),
       'code_num':('INC101','INC102','INC103','INC104','INC105','INC106','INC201','INC202','INC203','INC301','INC302','INC303'),
       'days':('4','18','9','15','3','6','10','5','3','1','8','5'),
       'products': ('remote','antenna','remote,antenna','TV','display','TV','display,touchpad','speaker','Cell','display','speaker','antenna')
}
df = pd.DataFrame(dct)
df.set_index(['id','Store','code_num'])

df : df:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM