[英]Pandas Pivot Table MultiIndex Columns to Single Level
I have a pandas dataframe which looks like as follows:我有一个如下所示的熊猫数据框:
df =
COLUMN_NAME YEAR1 YEAR2 VALUE
0 Column1 2013 2014 0.042835
1 Column1 2014 2015 0.033600
2 Column2 2013 2014 0.004406
3 Column2 2014 2015 0.016900
...
Where for each COLUMN_NAME, YEAR1 and YEAR2, a VALUE is calculated.对于每个 COLUMN_NAME、YEAR1 和 YEAR2,都会计算一个 VALUE。 I want to group the dataframe such that is it unique on COLUMN_NAME, where the columns look like the following:我想对数据框进行分组,使其在 COLUMN_NAME 上是唯一的,其中的列如下所示:
df_desired =
COLUMN_NAME Value_from_2013_2014 Value_from_2014_2015 ...
0 Column1 0.042835 0.033600
1 Column2 0.004406 0.016900
...
I can achieve sort of what I want with the code below, but it creates a MultiIndex columns, how can I achieve this?我可以使用下面的代码实现我想要的东西,但它创建了一个 MultiIndex 列,我该如何实现呢? Thanks for the help.谢谢您的帮助。
pd.pivot_table(df, 'VALUE', 'COLUMN_NAME', ['YEAR1', 'YEAR2'])
YEAR1 2013 2014
YEAR2 2014 2015
COLUMN_NAME
Column1 0.042835 0.0336
Column2 0.004406 0.0169
You can flatten the multiindex columns using to_flat_index
, then map
to str
and add your prefix:您可以使用to_flat_index
展平多to_flat_index
列,然后map
到str
并添加您的前缀:
s.columns = ["Value_from_"+"_".join(map(str, i)) for i in s.columns.to_flat_index()]
print (s)
Value_from_2013_2014 Value_from_2014_2015
COLUMN_NAME
Column1 0.042835 0.0336
Column2 0.004406 0.0169
df_agg = pd.pivot_table(df, 'VALUE', 'COLUMN_NAME', ['YEAR1', 'YEAR2'])
df_agg.columns = ['Value_from_' + str(df_agg.columns[i][0]) + '_' + str(df_agg.columns[i][1]) for i in range(len(df_agg.columns))]
If I understanding correctly, you are getting the desired values, but not the desired labels.如果我理解正确,您将获得所需的值,但不是所需的标签。 If so, you can change the column names with df_desired.columns = ['Value_from_2013_2014', 'Value_from_2014_2015']
如果是这样,您可以使用df_desired.columns = ['Value_from_2013_2014', 'Value_from_2014_2015']
更改列名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.