XaviorL
XaviorL

Reputation: 373

Python/Pandas: Remove unwanted text in a column

I wish to remove some unwanted text in a column, here is an example of the dataframe:

df_test = pd.DataFrame({'Id': [1, 2, 3, 4, 5, 6],
                        'value': ['12646586.0125284002019-11-25W001382021-02-25FORWARD',
                                  '12646586.0125284002019-11-25W001382021-02-25FORWARD',
                                  '12646586.0125284002019-11-25W001382021-02-25FORWARD',
                                  '12646586.05116292020-01-28W001382021-02-25FORWARD',
                                  '12646586.05116292020-01-28W001382021-02-25FORWARD',
                                  '12646586.05116292020-01-28W001382021-02-25FORWARD']})

I want to remove the ".0" only in the column 'value', ideally it should be like this:

   Id                                              value
0   1  12646586125284002019-11-25W001382021-02-25FORWARD
1   2  12646586125284002019-11-25W001382021-02-25FORWARD
2   3  12646586125284002019-11-25W001382021-02-25FORWARD
3   4    126465865116292020-01-28W001382021-02-25FORWARD
4   5    126465865116292020-01-28W001382021-02-25FORWARD
5   6    126465865116292020-01-28W001382021-02-25FORWARD

Only the '.0' removed from the string in the column. I tried code like this:

df_test['value'] = df_test['value'].str.replace("\s.0", "")

It did not work, so please help if possible. Much appreciate it.

Upvotes: 0

Views: 961

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

The \s.0 pattern matches a whitespace, then any char but a line break char, and a 0.

You wanted to match a dot and a zero:

df_test['value'] = df_test['value'].str.replace(r"\.0", "")
>>> df_test
   Id                                              value
0   1  12646586125284002019-11-25W001382021-02-25FORWARD
1   2  12646586125284002019-11-25W001382021-02-25FORWARD
2   3  12646586125284002019-11-25W001382021-02-25FORWARD
3   4    126465865116292020-01-28W001382021-02-25FORWARD
4   5    126465865116292020-01-28W001382021-02-25FORWARD
5   6    126465865116292020-01-28W001382021-02-25FORWARD

Upvotes: 2

Related Questions