8

I have a MultiIndex csv file which I would like to read in.

The data is saved in the csv file as follows:

import pandas as pd
import numpy as np

dfcsv = pd.read_csv("/FilePath/MultiIndex_Example.csv")
dfcsv

Which essentially leads to a data frame below:

enter image description here

Python Dataframe construction below: (easy reconstruction)

d = {'Country': ['City', 'PostCode','Day1','Day2','Day3'], 'UK': ['London', '123',47,42,40],'USA': ['New York', '456',31,22,58]}
dfstd = pd.DataFrame(data=d)

However, when I read in the data I need the 1st column to act as the multiIndex. Essentially creating a data frame as below:

arrays = [['UK','USA'],['London','New York'],['123','456']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['Country', 'City','Postcode'])
df = pd.DataFrame(np.random.randn(3, 2), index=['Day1', 'Day2', 'Day3'], columns=index)
df.columns 

enter image description here

I was wondering if there is a simple way of achieving this via pd.read_csv or a pd.MultIndex construction ?

FYI I tried the below but couldn't get it working Load CSV to Pandas MultiIndex DataFrame

1
  • just use headers as [0,1] Commented Jun 3, 2018 at 11:43

1 Answer 1

20

I think the following is what you need:

dfcsv = pd.read_csv("/FilePath/MultiIndex_Example.csv", index_col=[0], header=[0,1,2])

Here, index_col will take your first column which is 0 as index and header as 1st and 2nd row as header's which are 0,1,2 as its 0-indexed

Sign up to request clarification or add additional context in comments.

2 Comments

Ah this nearly works, if you add header=[0,1,2] then it works perfectly!
@RK1 - Sorry, I think 3 row is data, answer was edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.