Reputation: 4554
I want to replace a part of string in a dataframe. For example,
df =
index group
2019-02-01 09:30:07 8157_S1
2019-02-01 09:30:23 S_1.M_01
2019-02-01 09:30:38 S_1.M_02
2019-02-01 09:32:11 S_1.M_08
2019-02-01 09:32:27 S_1.M_12
2019-02-01 09:32:42 S_1.M_11
2019-02-01 09:32:57 S_1.M_10
2019-02-01 09:33:13 S_1.M_09
2019-02-01 09:33:30 8157_S2
2019-02-01 09:33:47 S_2.M_13
I want to remove "8157_" and "S_1." and "S_2.". My present code is
df['group'] = df['group'].replace("8157_", "")
df['group'] = df['group'].replace("S_1.","")
There was no change in the output.
But my expected output is
df =
index group
2019-02-01 09:30:07 S1
2019-02-01 09:30:23 M_01
2019-02-01 09:30:38 M_02
2019-02-01 09:32:11 M_08
2019-02-01 09:32:27 M_12
2019-02-01 09:32:42 M_11
2019-02-01 09:32:57 M_10
2019-02-01 09:33:13 M_09
2019-02-01 09:33:30 S2
2019-02-01 09:33:47 M_13
Upvotes: 0
Views: 53
Reputation: 29732
Using pandas.Series.str.replace
:
df['group'] = df['group'].str.replace("S_[12]\.|8157_","")
print(df)
Output:
index group
0 2019-02-01 09:30:07 S1
1 2019-02-01 09:30:23 M_01
2 2019-02-01 09:30:38 M_02
3 2019-02-01 09:32:11 M_08
4 2019-02-01 09:32:27 M_12
5 2019-02-01 09:32:42 M_11
6 2019-02-01 09:32:57 M_10
7 2019-02-01 09:33:13 M_09
8 2019-02-01 09:33:30 S2
9 2019-02-01 09:33:47 M_13
Upvotes: 1