将多索引数据帧拆散到pandas中的平面数据框

时间:2014-04-01 07:35:00

标签: python pandas ipython

我在pandas中有一个名为groupt3的多索引df,当我输入groupt3.head()时,它看起来像这样:

                datetime     song   sum   rat
artist datetime
2562     8      2            2      26    0
         46     19           19     26    0
         47     3            3      26    0
4Hero    1      2            2      32    0
         26     20           20     32    0
         9      10           10     32    0

我想要一个" flat"取得艺术家索引和日期时间索引的数据框和"重复它"形成这个:

artist     date time    song   sum   rat
2562       8            2      26    0
2562       46           19     26    0
2562       47           3      26    0

等...

感谢。

2 个答案:

答案 0 :(得分:8)

使用pandas.DataFrame.to_records()

示例:

import pandas as pd
import numpy as np
arrays = [['Monday','Monday','Tursday','Tursday'],
                        ['Morning','Noon','Morning','Evening']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['Weekday', 'Time'])
df = pd.DataFrame(np.random.randint(5, size=(4,2)), index=index)

In [39]: df
Out[39]: 
                 0  1
Weekday Time         
Monday  Morning  1  3
        Noon     2  1
Tursday Morning  3  3
        Evening  1  2

In [40]: pd.DataFrame(df.to_records())
Out[40]: 
   Weekday     Time  0  1
0   Monday  Morning  1  3
1   Monday     Noon  2  1
2  Tursday  Morning  3  3
3  Tursday  Evening  1  2

答案 1 :(得分:2)

我认为您可以使用reset_index

import pandas as pd
import numpy as np

np.random.seed(0)
arrays = [['Monday','Monday','Tursday','Tursday'],
                        ['Morning','Noon','Morning','Evening']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['Weekday', 'Time'])
df = pd.DataFrame(np.random.randint(5, size=(4,2)), index=index)
print df
                 0  1
Weekday Time         
Monday  Morning  4  0
        Noon     3  3
Tursday Morning  3  1
        Evening  3  2

print df.reset_index()
   Weekday     Time  0  1
0   Monday  Morning  4  0
1   Monday     Noon  3  3
2  Tursday  Morning  3  1
3  Tursday  Evening  3  2