Pandas: Modify a particular level of Multiindex

Thanks to @cxrodgers’s comment, I think the fastest way to do this is: df.index = df.index.set_levels(df.index.levels[0].str.replace(‘ ‘, ”), level=0) Old, longer answer: I found that the list comprehension suggested by @Shovalt works but felt slow on my machine (using a dataframe with >10,000 rows). Instead, I was able to use .set_levels method, which was quite … Read more

Filling in date gaps in MultiIndex Pandas Dataframe

You can make a new multi index based on the Cartesian product of the levels of the existing multi index. Then, re-index your data frame using the new index. new_index = pd.MultiIndex.from_product(df.index.levels) new_df = df.reindex(new_index) # Optional: convert missing values to zero, and convert the data back # to integers. See explanation below. new_df = … Read more

Giving a column multiple indexes/headers

You can use multiIndex to give multiple columns with names for each level. Use MultiIndex.from_product() to make multiIndex from cartesian products of multiple iterables. header = pd.MultiIndex.from_product([[‘location1′,’location2’], [‘S1′,’S2′,’S3’]], names=[‘loc’,’S’]) df = pd.DataFrame(np.random.randn(5, 6), index=[‘a’,’b’,’c’,’d’,’e’], columns=header) Two levels will be loc and S. df loc location1 location2 S S1 S2 S3 S1 S2 S3 a -1.245988 … Read more

Pandas – write Multiindex rows with to_csv

I think this will do it In [3]: df = DataFrame(dict(A = ‘foo’, B = ‘bar’, value = 1),index=range(5)).set_index([‘A’,’B’]) In [4]: df Out[4]: value A B foo bar 1 bar 1 bar 1 bar 1 bar 1 In [5]: df.to_csv(‘test.csv’) In [6]: !cat test.csv A,B,value foo,bar,1 foo,bar,1 foo,bar,1 foo,bar,1 foo,bar,1 In [7]: pd.read_csv(‘test.csv’,index_col=[0,1]) Out[7]: value … Read more

Pandas: combining header rows of a multiIndex DataFrame

1. Update using Python 3.6+ use f-string formatting with list comprehension: df.columns = [f'{i}{j}’ for i, j in df.columns] 2. Use map and join: df.columns = df.columns.map(”.join) 3. If your columns has numeric datatypes, use map and format: df.columns = df.columns.map(‘{0[0]}{0[1]}’.format) Output: barone bartwo bazone baztwo fooone footwo foothree A 1 2 3 4 5 … Read more