在pivot_table之后将熊猫多索引列名称转换为常规数据框列

时间:2019-09-12 21:27:18

标签: python pandas dataframe

我有一些使用pivot_table的数据,如下所示:

# Sample data
import pandas as pd
df = pd.DataFrame({
    'building': ['ab1', 'ab1', 'ab1', 'ab1', 'ab1', 'ab2', 'ab2', 'ab2', 'ab2'],
    'year': [2017, 2017, 2017, 2018, 2018, 2017, 2017, 2018, 2018],
    'unit': ['blah', 'blah', 'crap', 'blah', 'crap', 'blah', 'crap', 'blah', 'crap'], 
    'value1': [5.1, 5.3, 4.5, 5.7, 4.7, 3.8, 4.9, 6.0, 5.3],
    'value2': [100, 102, 110, 105, 106, 112, 107, 111, 102]
})

df
    building    year    unit    value1  value2
0   ab1         2017    blah    5.1     100
1   ab1         2017    blah    5.3     102
2   ab1         2017    crap    4.5     110
3   ab1         2018    blah    5.7     105
4   ab1         2018    crap    4.7     106
5   ab2         2017    blah    3.8     112
6   ab2         2017    crap    4.9     107
7   ab2         2018    blah    6.0     111
8   ab2         2018    crap    5.3     102

# Use pivot_table to cast out unit
df2 = pd.pivot_table(df, index=['building', 'year'], values=['value1', 'value2'], columns='unit').reset_index()
df2
    building    year    value1          value2
unit                    blah    crap    blah    crap
0   ab1        2017      5.2    4.5      101    110
1   ab1        2018      5.7    4.7      105    106
2   ab2        2017      3.8    4.9      112    107
3   ab2        2018      6.0    5.3      111    102

但是,df2是一种奇怪的非数据帧格式。我怎样才能得到像下面这样的东西?

    building    year    blah_value1 crap_value1 blah_value2 crap_value2
0   ab1         2017         5.2       4.5         101        110
1   ab1         2018         5.7       4.7         105        106
2   ab2         2017         3.8       4.9         112        107
3   ab2         2018         6.0       5.3         111        102

0 个答案:

没有答案