简体   繁体   English

pandas pivot 表问题 - 假设我是如何构建它的?

[英]pandas pivot table issue - assuming it is how i am structuring it?

i have a dataset that contains video game platforms, and the year that games were released for it.我有一个包含视频游戏平台的数据集,以及为其发布游戏的年份。

what i'm trying to do is end up with a dataframe that has the count of titles for each year released by platform.我想要做的是最终得到一个 dataframe ,其中包含平台每年发布的标题数量。

my initial dataframe looks like this:我最初的 dataframe 看起来像这样:

platform    year
0   Wii     2006.0
1   NES     1985.0
2   Wii     2008.0
3   Wii     2009.0
4   GB      1996.0
5   GB      1989.0
6   DS      2006.0
7   Wii     2006.0
8   Wii     2009.0
9   NES     1984.0
10  DS      2005.0
11  DS      2005.0
12  GB      1999.0
13  Wii     2007.0
14  X360    2010.0
15  Wii     2009.0
16  PS3     2013.0
17  PS2     2004.0
18  SNES    1990.0
19  DS      2005.0

i'm using a groupby to get them together:我正在使用 groupby 将它们放在一起:

df = df.sort_values(['year']).groupby(['year'])['platform'].value_counts()

which gets me close:这让我很接近:

year           platform
1980.0           2600         9
1981.0           2600        46
1982.0           2600        36
1983.0           2600        11
                 NES          6
1984.0           NES         13
                 2600         1
1985.0           NES         11
                 2600         1
                 DS           1

but this is a series, and with the year being the index i can't stick this into something like a heatmap.但这是一个系列,以年份为索引,我无法将其粘贴到热图之类的东西中。

here is an example of the desired output:这是所需 output 的示例:

   year platform  #_titles
1980    2600        9
1981    2600        46
1982    2600        36
1983    2600        11
1983    NES         6
1984    NES         13
1984    2600        1
1985    NES         11
1985    2600        1
1985    DS          1
1985    PC          1
1986    NES         19
1986    2600        2
1987    NES         10
1987    2600        6
1988    NES         11
1988    2600        2
1988    GB          1
1988    PC          1
1989    GB          10

I was thinking i might need to use a pivot_table() but this is something i am still quite new to and am struggling to implement.我在想我可能需要使用 pivot_table() ,但这是我仍然很陌生并且正在努力实现的东西。

i tried something like:我试过类似的东西:

df = df.pivot_table(df,index='year',columns = 'platform',aggfunc = 'count') 

but my output then is just the year.但我的 output 那时只是一年。

clearly i am doing something wrong, and figure it is time to stop beating my virtual head on juypter notebook and ask for some advice.显然我做错了什么,并且认为是时候停止在 juypter 笔记本上敲打我的虚拟头并寻求一些建议。

I am fine with getting the original group method to work, or using a pivot table either way - I just would appreciate some pointers on what i'm doing wrong so i can correct it.我可以让原始组方法正常工作,或者以任何一种方式使用 pivot 表 - 我只是希望能得到一些关于我做错了什么的指示,以便我可以纠正它。

Thanks for your time in advance,提前感谢您的时间,

Jared杰瑞德

edit: here is the result from the first answer (which would be perfect, if it had the aggfunc in it? not sure why that isn't there?): |year|platform|编辑:这是第一个答案的结果(如果里面有 aggfunc 那就完美了?不知道为什么不存在?):|year|platform| |----|--------| |----|--------| |1980.0|2600| |1980.0|2600| |1981.0|2600| |1981.0|2600| |1982.0|2600| |1982.0|2600| |1983.0|2600 ||NES| |1983.0|2600 ||NES| |1984.0|2600| |1984.0|2600| ||NES| ||NES|

Here is the solution with pivot table:这是 pivot 表的解决方案:

res = pd.pivot_table(df,index=['year', 'platform'],aggfunc = 'size')

>>> print(res)

year    platform
1984.0  NES         1
1985.0  NES         1
1989.0  GB          1
1990.0  SNES        1
1996.0  GB          1
1999.0  GB          1
2004.0  PS2         1
2005.0  DS          3
2006.0  DS          1
        Wii         2
2007.0  Wii         1
2008.0  Wii         1
2009.0  Wii         3
2010.0  X360        1
2013.0  PS3         1

Maybe this is what you want?也许这就是你想要的? Hard to tell since your output doesn't match the input.很难说,因为您的 output 与输入不匹配。

df.sort_values(['year']).groupby(['year','platform']).size().reset_index(name='#_titles')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM