Home > Software engineering >  Replacing extension in the Pandas dataframe
Replacing extension in the Pandas dataframe

Time:02-10

I have a pandas dataframe containing song url with '.mp3' file extension. I want to replace '.wav'

The dataframe looks like this enter image description here

enter image description here

Here you can see that, Path column contains the path of the song in the Songs directory. The song has '.mp3' extension. But I want to replace it with '.wav' extension.

How should I do it?

CodePudding user response:

We can try this one :

df['Path'] = df['Path'].str.replace('.mp3', '.wav', regex=False)

And if we want to be sure that we want to change the .mp3 at the end of the string :

df['Path'] = df['Path'].str.replace('.mp3$', '.wav', regex=True)
  •  Tags:  
  • Related