[英]Rename string values automatically in pandas
I have a dataframe like this:我有一个这样的 dataframe:
Var Name![]() |
Val![]() |
---|---|
a![]() |
11 ![]() |
a![]() |
1 ![]() |
a![]() |
2 ![]() |
b ![]() |
3 ![]() |
b ![]() |
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 ![]() |
a1 ![]() |
a![]() |
1 ![]() |
a2 ![]() |
a![]() |
2 ![]() |
a3 ![]() |
b ![]() |
3 ![]() |
b1 ![]() |
b ![]() |
4 ![]() |
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.