I have several .csv files that I would like to read as MultiIndexed DataFrames, but the spanning column heading does not repeat, and therefore I am left with two headers, not MultiIndex.
File test.csv:
A,,B,,C,
a1,a2,b1,b2,c1,c2
1,1,1,1,1,1
2,2,2,2,2,2
When I run the following,
import pandas as pd
df = pd.read_csv('test.csv', header=[0,1])
print(df)
The returned structure is not what I am looking for:
A Unnamed: 1_level_0 B Unnamed: 3_level_0 C Unnamed: 5_level_0
a1 a2 b1 b2 c1 c2
0 1 1 1 1 1 1
1 2 2 2 2 2 2
I need a MultiIndex with the first column header acting as follows:
A B C
a1 a2 b1 b2 c1 c2
0 1 1 1 1 1 1
1 2 2 2 2 2 2
Is there any way to read in csv as-is to get the desired structure? If not, this is the most efficient way to do this just to modify the csv files so that they explicitly repeat the values โโof the external header, how is it?
A,A,B,B,C,C
a1,a2,b1,b2,c1,c2
1,1,1,1,1,1
2,2,2,2,2,2