简体   繁体   中英

How to read MultiIndex group data from excel using pandas python

I am having data like below

在此处输入图像描述

I need to fetch day-wise data based on locations for each name.

I know how to fetch data from excel if it is having single row header but I am not getting how to fetch data from mentioned excel format format.

could anyone help me to solve this so it will help me to do my further work.

I want to achieve something like this.

import pandas as pd

df = pd.read_excel('test.xlsx', header=[0,1], index_col=[0], sheet_name='Sheet1')
print(df)

lst = []
for i in range(0, len(df['Location1'])):
    dict = {}
    dict["name"] = df['Name'][i]
    dict["location1_day1"] = df['Location1']['Day1'][i]
    dict["location1_day2"] = df['Location1']['Day2'][i]
    dict["location2_day1"] = df['Location2']['Day1'][i]
    dict["location2_day2"] = df['Location2']['Day2'][i]
    lst.append(dict)

print(lst)

expected output

Name = Rose, Location1_Day1 = 1, Location1_Day2 = 2, Location1_Day3 = 3,Location2_Day1 = 11, Location2_Day2 = 12, Location2_Day3 = 13,
Name = Jasmine, Location1_Day1 = 4, Location1_Day2 = 5, Location1_Day3 = 6,Location2_Day1 = 14, Location2_Day2 = 15, Location2_Day3 = 16
import pandas as pd
df = pd.read_excel('../test.xlsx', 
    header=[0,1], index_col=[0], sheet_name='Sheet1')

the header option allows you to specify a list of rows corresponding to the column names. index_col allows you to specify the index column, and specifies that name does not have multiple-column keys. the dataframe can be indexed like this:

df['Location 1']['Day2']

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