简体   繁体   English

如何显示 dataframe 的 MultiIndex object?

[英]How do I display a MultiIndex object of a dataframe?

I had a data frame jobs_2 which has many columns, amongst which are 'city' and 'state'.我有一个数据框jobs_2,它有很多列,其中是“城市”和“州”。

From jobs_2 I created a new data frame 'jobs_2i' where state and city are set as indices of jobs_2.从jobs_2我创建了一个新的数据框'jobs_2i',其中state和city被设置为jobs_2的索引。

For that I used the code:为此,我使用了代码:

jobs_2i = jobs_2.set_index(['state', 'city'])

I now need to use an appropriate property of data frames to display the MultiIndex object of jobs_2i, but I am unsure how to do that.我现在需要使用数据框的适当属性来显示 jobs_2i 的 MultiIndex object,但我不确定如何做到这一点。

I'm not sure exactly what you are asking for, but here is a quick example of using a MultiIndex:我不确定你到底要什么,但这里有一个使用 MultiIndex 的简单示例:

df = pd.DataFrame(data).set_index(['state', 'city'])
df.loc['California']  # Gets me everything with state is california
df.loc[('California', 'Los Angeles')]  # Gets me everything in Los Angeles, CA

Is that what you were looking to solve?这就是你想要解决的问题吗? Otherwise, can you try and give me an example?否则,你能不能给我举个例子?

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

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