[英]Loop to remove the same letter of each row
In Python, if I want to add a letter in each row of a column, I use this: 在Python中,如果我想在列的每一行中添加一个字母,请使用以下命令:
df = df.assign(Meses=[f'M{i}' for i in df.Meses])
Meses
1 M1
2 M2
3 M3
4 M4
5 M5
6 M6
7 M7
8 M8
9 M9
10 M10
11 M11
How can I do the opposite (remove the "m" of each row of a certain column)? 我该如何做相反的操作(删除某一列的每一行的“ m”)?
This should be much faster: 这应该快得多:
df['Meses'] = df['Meses'].astype(str).str[1:]
Output: 输出:
Meses
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
df = df.assign(Meses=[mi[1:] for mi in df.Meses])
should work. 应该管用。
EDIT: But as suggested by @harvpan, 编辑:但正如@harvpan建议的那样,
df['Meses'] = df['Meses'].astype(str).str[1:]
might be consequently faster because it is entirely pandas native. 因此可能会更快,因为它完全是熊猫原生的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.