具有重复索引的数据透视表

时间:2020-04-12 10:52:42

标签: python pandas indexing pivot-table

我有下表:

name                    year    code                     value
EISMA MEDIA GROEP B.V.  2017    IS_030_EBITDA            2303.0
EISMA MEDIA GROEP B.V.  2017    IS_09_PostTaxResult      1292.0
EISMA MEDIA GROEP B.V.  2018    IS_030_EBITDA            2280.0
EISMA MEDIA GROEP B.V.  2018    IS_09_PostTaxResult      1324.0
DUNLOP B.V.             2017    IS_030_EBITDA            43433.0
DUNLOP B.V.             2017    IS_09_PostTaxResult      1243392.0
DUNLOP B.V.             2018    IS_030_EBITDA            2243480.0
DUNLOP B.V.             2018    IS_09_PostTaxResult      1324.0

我想做一个数据透视表,以便每个公司根据年份只有两行。

所以预期的输出将是:

name                    year   IS_030_EBITDA  IS_09_PostTaxResult
EISMA MEDIA GROEP B.V.  2017   2303.0         1292.0
EISMA MEDIA GROEP B.V.  2018   2280.0         1324.0
DUNLOP B.V.             2017   43433.0        1243392.0
DUNLOP B.V.             2018   2243480.0      1324.0

如果仅考虑一家公司,则以下行有效:

df.pivot(index='year', columns='code', values = 'value')

但这不适用于两家公司。

有人可以帮我吗?

谢谢

0 个答案:

没有答案
相关问题