我该怎么做:

 year artist           genre    genre_sales
 1999      A        Pop/Rock             10
 1999      B        Hip/Hop              15
 1999      C        Country               8
 2000      A        Pop/Rock             11
 2000      B        Hip/Hop              14
 2000      D        Jazz                  1
 2001      B        Hip/Hop              18
 2001      C        Country              10

进入这个:
 year artist           genre    genre_sales
 1999      A        Pop/Rock            Nan
 1999      B        Hip/Hop             Nan
 1999      C        Country             Nan
 2000      A        Pop/Rock             10
 2000      B        Hip/Hop              15
 2000      D        Jazz            (1999 Jazz sales despite D not releasing in '99)
 2001      B        Hip/Hop              14
 2001      C        Country         (2000 country values, not the 8 from '99)

我看到了groupby转移问题和食谱页面,并假设我可以做如下事情:
df.groupby(['year','artist'])['genre_sales'].shift(1)


df.groupby(['year','genre','artist'])['genre_sales'].shift(1)

但我认为,只有艺术家每年制作一张专辑,这些才能奏效。
当前的“流派销售”是指当年艺术家流派的年度销售额。我正试图让上一年的“流派销售”与每个艺术家连成一排。每种类型每年都会有多个艺术家,每个艺术家在给定的一年中可以有零个或多个该类型的作品。

最佳答案

df_prev = df.copy()

df[     'prev_year'] = df[     'year'] - 1
df_prev['prev_year'] = df_prev['year']
df_prev[     'year'] = df_prev['year'] + 1

df2 = df.merge( df_prev, how='outer', on=['year','prev_year','artist','genre'],
                suffixes=['','_prev'] )

df2.sort(['artist','genre','year']).fillna(0)

    year artist     genre  genre_sales  prev_year  genre_sales_prev
0   1999      A  Pop/Rock           10       1998                 0
3   2000      A  Pop/Rock           11       1999                10
9   2001      A  Pop/Rock            0       2000                11
1   1999      B   Hip/Hop           15       1998                 0
4   2000      B   Hip/Hop           14       1999                15
6   2001      B   Hip/Hop           18       2000                14
11  2002      B   Hip/Hop            0       2001                18
2   1999      C   Country            8       1998                 0
8   2000      C   Country            0       1999                 8
7   2001      C   Country           10       2000                 0
12  2002      C   Country            0       2001                10
5   2000      D      Jazz            1       1999                 0
10  2001      D      Jazz            0       2000                 1

关于python - groupby转移 Pandas ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32661742/

10-12 20:56