In Python, if I want to add a letter in each row of a column, I use this:
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)?
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,
df['Meses'] = df['Meses'].astype(str).str[1:]
might be consequently faster because it is entirely pandas native.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.