Converting Index into MultiIndex (hierarchical index) in Pandas

后端 未结 3 1254
广开言路
广开言路 2021-02-04 08:49

In the data I am working with the index is compound - i.e. it has both item name and a timestamp, e.g. name@domain.com|2013-05-07 05:52:51 +0200.

I want to

3条回答
  •  你的背包
    2021-02-04 08:52

    In pandas>=0.16.0, we can use the .str accessor on indices. This makes the following possible:

    df.index = pd.MultiIndex.from_tuples(df.index.str.split('|').tolist())
    

    (Note: I tried the more intuitive: pd.MultiIndex.from_arrays(df.index.str.split('|')) but for some reason that gives me errors.)

提交回复
热议问题