简体   繁体   中英

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'.

From jobs_2 I created a new data frame 'jobs_2i' where state and city are set as indices of 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.

I'm not sure exactly what you are asking for, but here is a quick example of using a 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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