python - Converting Index into MultiIndex (hierarchical index) in Pandas -
in data working index compound - i.e. has both item name , timestamp, e.g. name@domain.com|2013-05-07 05:52:51 +0200
.
i want hierarchical indexing, same e-mails grouped together, need convert dataframe index multiindex (e.g. entry above - (name@domain.com, 2013-05-07 05:52:51 +0200)
).
what convenient method so?
once have dataframe
import pandas pd df = pd.read_csv("input.csv", index_col=0) # or source
and function mapping each index tuple (below, example question)
def process_index(k): return tuple(k.split("|"))
we can create hierarchical index in following way:
df.index = pd.multiindex.from_tuples([process_index(k) k,v in df.iterrows()])
an alternative approach create 2 columns set them index (the original index dropped):
df['e-mail'] = [x.split("|")[0] x in df.index] df['date'] = [x.split("|")[1] x in df.index] df = df.set_index(['e-mail', 'date'])
or shorter
df['e-mail'], df['date'] = zip(*map(process_index, df.index)) df = df.set_index(['e-mail', 'date'])
Comments
Post a Comment