简体   繁体   English

在 pandas 中自动重命名字符串值

[英]Rename string values automatically in pandas

I have a dataframe like this:我有一个这样的 dataframe:

Var Name变量名 Val瓦尔
a一种 11 11
a一种 1 1个
a一种 2 2个
b b 3 3个
b b 4 4个

I would like to add a column with updated and enumerated "Var Name", something like this我想添加一个包含更新和枚举的“Var Name”的列,就像这样

Var Name变量名 Val瓦尔 Var Name变量名
a一种 11 11 a1 a1
a一种 1 1个 a2 a2
a一种 2 2个 a3 a3
b b 3 3个 b1 b1
b b 4 4个 b2 b2

My idea is to enumerate the Var Name (1,2,3...) till it "recognizes" a new Var Name and start enumerating again from 1.我的想法是枚举 Var Name (1,2,3...)直到它“识别”一个新的Var Name and从 1 开始再次枚举。

groupby + cumcount is what you need here: groupby + cumcount是你在这里需要的:

df['Var Name2'] = df['Var Name'] + df.groupby('Var Name').cumcount().add(1).astype(str)

Output: Output:

>>> df
  Var Name  Val Var Name2
0        a   11        a1
1        a    1        a2
2        a    2        a3
3        b    3        b1
4        b    4        b2

To add extra characters, such as an underscore:要添加额外的字符,例如下划线:

df['Var Name2'] = df['Var Name'] + '_' + df.groupby('Var Name').cumcount().add(1).astype(str)

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

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