如何使用pivot重塑数据帧?

2024-03-29 10:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框

    Year  Start  End   Name       Price
0   nan   0101   0331  Squirtle    876
1  2021   0101   1231  Squirtle    200
2   nan   0101   0331  Wartortle   1000
3  2021   0101   1231  Wartortle   1200
4   nan   0101   0331  Blastoise   3100
5  2021   0101   1231  Blastoise   4200
6  2022   0101   1231  Blastoise   10000

我想把它改造成这样

                   Name    Squirtle      Wartortle       Blastoise
Year  Start End
nan   0101  0331              876           1000            3100
2021  0101  1231              200           1200            4200
2022  0101  1231                                            10000

我试过了, df.pivot(index=['Year', 'Start', 'End'], columns='Name', values='Price')。但是没有得到任何运气。 任何帮助都将不胜感激


Tags: columns数据namedfindexnanyearstart
3条回答

我认为你错用了pivot而不是pivot_table

data = [[np.NaN, 101, 331, 'Squirtle', 876],
[2021, 101, 1231, 'Squirtle', 200],
[np.NaN, 101, 331, 'Wartortle', 1000],
[2021, 101, 1231, 'Wartortle', 1200],
[np.NaN, 101, 331, 'Blastoise', 3100],
[2021, 101, 1231, 'Blastoise', 4200],
[2022, 101, 1231, 'Blastoise', 10000]]

df.pivot_table(index=['Year', 'Start', 'End'], columns='Name', values='Price')

产出:

Name               Blastoise  Squirtle  Wartortle
Year   Start End                                 
2021.0 101   1231     4200.0     200.0     1200.0
2022.0 101   1231    10000.0       NaN        NaN

而如果用占位符值(如1000)替换这些值

df = df.fillna(1000)
df.pivot_table(index=['Year', 'Start', 'End'], columns='Name', values='Price')

你会得到你想要的:

Name               Blastoise  Squirtle  Wartortle
Year   Start End                                 
1000.0 101   331      3100.0     876.0     1000.0
2021.0 101   1231     4200.0     200.0     1200.0
2022.0 101   1231    10000.0       NaN        NaN

使用set_indexunstack

df.set_index(['Year','Start','End','Name'])['Price'].unstack()

输出:

Name               Blastoise  Squirtle  Wartortle
Year   Start End                                 
NaN    101   331      3100.0     876.0     1000.0
2021.0 101   1231     4200.0     200.0     1200.0
2022.0 101   1231    10000.0       NaN        NaN

你很接近。使用pivot_table而不是pivot来获取所需的分组。唯一需要注意的是,您需要替换NA值(如果它们实际上是NA而不是字符串'nan'

df.fillna('NA').pivot_table(index=['Year', 'Start', 'End'], columns='Name', values='Price')
# returns:
Name               Blastoise  Squirtle  Wartortle
Year   Start End
2021.0 101   1231     4200.0     200.0     1200.0
2022.0 101   1231    10000.0       NaN        NaN
NA     101   331      3100.0     876.0     1000.0

相关问题 更多 >