-1

I have this dataframe :

df=pd.DataFrame({'a': [2, 6, 8, 9],
                'date': ['2021-07-21 04:34:02', 
                         'test_2022-17-21 04:54:22',
                        'test_2020-06-21 04:34:02',
                        '2023-12-01 11:54:52']})

df["date"].replace("test_", "")
df

I would like to delete 'test_' from the column date. Maybe, you can help

ahmedaao
  • 377
  • 1
  • 3
  • 14

2 Answers2

3

Use str.strip(<unnecessary string>) to remove the unnecessary string:

df.date = df.date.str.strip('test_')

OUTPUT:

   a                 date
0  2  2021-07-21 04:34:02
1  6  2022-17-21 04:54:22
2  8  2020-06-21 04:34:02
3  9  2023-12-01 11:54:52
Nk03
  • 14,699
  • 2
  • 8
  • 22
0

The same question was answered here. check the link. For your specific inquiry, this one line is all you want.

df['date'] = df['date'].map(lambda x: x.lstrip('test_'))
dougMcarthy
  • 49
  • 1
  • 5