Siraj S.
Siraj S.

Reputation: 3751

strip whitespace in pandas MultiIndex index columns values

i have a MultiIndex dataframe of mix datatypes. One of the index columns values has trailing whitespaces. How can i strip these trailing whitespace for the index columns. here is the sample code:

import pandas as pd
idx = pd.MultiIndex.from_product([['1.0'],['NY   ','CA  ']], names=['country_code','state'])
df = pd.DataFrame({'temp':['78','85']},index = idx)

One solution is to reset the index, strip the whitespaces for the desired column and again set index. Something like below:

df = df.reset_index()
df['state'] = df['state'].str.strip()
df =  df.set_index(['country_code','state'],drop=True)

but this is a roundabout way, is there a more direct way to strip the whitespaces in the index itself?

Upvotes: 0

Views: 1891

Answers (2)

wflynny
wflynny

Reputation: 18541

Similar to the other answer:

df.index.set_levels(df.index.map(lambda x: (x[0], x[1].strip())), inplace=True)

Upvotes: 2

akuiper
akuiper

Reputation: 215077

You can use .index.set_levels() and .index.get_level_values() to manipulate the index at a specific level:

df.index.set_levels(df.index.get_level_values(level = 1).str.strip(), 
                    level = 1, inplace=True)

df.index
# MultiIndex(levels=[['1.0'], ['NY', 'CA']],
#            labels=[[0, 0], [1, 0]],
#            names=['country_code', 'state'])

Upvotes: 2

Related Questions