首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >替换dataframe中某一列的值

替换dataframe中某一列的值
EN

Stack Overflow用户
提问于 2019-05-28 15:21:07
回答 1查看 28关注 0票数 1

将pandas作为pd导入numpy作为np导入ast

pd.options.display.max_columns = 20

我的数据框专栏季节看起来像这样(前20个条目):

代码语言:javascript
运行
复制
      season
0     2006-07
1     2007-08
2     2008-09
3     2009-10
4     2010-11
5     2011-12
6     2012-13
7     2013-14
8     2014-15
9     2015-16
10    2016-17
11    2017-18
12    2018-19
13     Career
14     season
15    2018-19
16     Career
17     season
18    2017-18
19    2018-19

它以赛季开始,以职业生涯结束。我想用从1开始到职业生涯结束的数字来代替年份。我想是这样的:

代码语言:javascript
运行
复制
      season
0     1
1     2
2     3
3     4
4     5
5     6
6     7
7     8
8     9
9     10
10    11
11    12
12    13
13     Career
14     season
15    1
16     Career
17     season
18    1
19    2

所以每次有赛季的时候,计数都应该重置,每次职业生涯结束的时候,计数都应该重置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-28 15:26:28

使用计数器的GroupBy.cumcountSeries.isin创建的掩码与移位值进行比较,从而创建连续的组:

代码语言:javascript
运行
复制
s = df['season'].isin(['Career', 'season'])
df['new'] = np.where(s, df['season'], df.groupby(s.ne(s.shift()).cumsum()).cumcount() + 1)
print (df)
     season     new
0   2006-07       1
1   2007-08       2
2   2008-09       3
3   2009-10       4
4   2010-11       5
5   2011-12       6
6   2012-13       7
7   2013-14       8
8   2014-15       9
9   2015-16      10
10  2016-17      11
11  2017-18      12
12  2018-19      13
13   Career  Career
14   season  season
15  2018-19       1
16   Career  Career
17   season  season
18  2017-18       1
19  2018-19       2

对于replace列season

代码语言:javascript
运行
复制
s = df['season'].isin(['Career', 'season'])
df.loc[~s, 'season'] = df.groupby(s.ne(s.shift()).cumsum()).cumcount() + 1
print (df)
    season
0        1
1        2
2        3
3        4
4        5
5        6
6        7
7        8
8        9
9       10
10      11
11      12
12      13
13  Career
14  season
15       1
16  Career
17  season
18       1
19       2
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56336934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档