anarchy
anarchy

Reputation: 5184

Replace a string in a column for each row of a pandas dataframe

Edit: I had to add this part to my original question as it's relevant.

I have a frame which contains many different prefixes in the name column called dfload.

I use the following command to create a slice called df.

df = dfload.loc[dfload['Name'].str.contains("testData/")]

Original Question continues from here:

Then, I have the following pandas dataframe called df,

   name               etc etc etc
0  testData/example1  etc ...
1  testData/example2  ...
2  testData/example3
3  testData/example4
...

I want to replace the string testData/ with nothing for the entire column so it looks like this

   name      etc etc etc
0  example1  etc ...
1  example2  ...
2  example3
3  example4
...

I used the following command df['name'] = df['name'].str.replace('testData/','').

But I get this error,

<ipython-input-20-dae746394d2d>:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df['name'] = df['name'].str.replace('testData/','')

The dataframe looks fine, why am I getting this error? What's the "proper" way to do this?

Upvotes: 1

Views: 3766

Answers (3)

EMiller
EMiller

Reputation: 837

to avoid the warning that you're getting, create df like this:

import pandas as pd
df = pd.DataFrame(dfload[dfload.name.str.contains('testdata/')])

specifying that it's a dataframe and not a slice is probably what keeps pandas from throwing the warning

Upvotes: 2

Daniela Varela
Daniela Varela

Reputation: 46

You should try using lamda funtion to apply the replace statement over every row:

df["name"]= df.apply(lambda x: x['name'].replace('testData/',''), axis=1)

Upvotes: 0

Oladimeji Mudele
Oladimeji Mudele

Reputation: 63

Use this:

df.name = df.name.str.replace('testData/','',regex = True)

Upvotes: 0

Related Questions