I am reading a .csv file that looks like this:
,1,1,2,2,3,3...
,'A','B','A','B',...
0,1,2,3,4,...
1,2,3,4,5,...
I read this with
df = pd.read_csv(fname,header=[0,1],index_col=0)
Now, deplorably, pandas sets the dtype of the first level of the column multiindex to string, even though there are only integers in the first row of the file. Unfortunately, there is no way to tell pandas what dtype to use for each row (level) of the column index. (Something like header={0:'int64',1:'string'}).
Now, it seems that there should be a simple and easy way to convert one level of the multiindex columns to int, but I searched for a long time and could not come up with anything. Right now, I am re-generating the index from scratch, but that seems overkill.
Another solution that could possibly work would be to convert the multiindex to a DataFrame, change the dtypes, then set the index from the DataFramce. That also seems like an overcomplicated process.
Suggestions?
You could use
set_levels: