1

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?

4
  • it's not really an error, though, it's a warning. Commented Jun 24, 2020 at 22:38
  • will I run into problems if I keep doing it this way then? Commented Jun 24, 2020 at 22:39
  • you shouldn't, but you might be able to suppress the warning with df.loc[:,['name']] = df.name.str.replace('testdata/','') Commented Jun 24, 2020 at 22:56
  • ok I think it's happening because I made this frame from a slice of another frame, is that why? I used this command df = dfload.loc[dfload['Name'].str.contains("testData/")] Commented Jun 24, 2020 at 23:00

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I've never made slices that way, it works!. learned something new thanks
@anarchy I think that the majority of the work was actually figuring out how to reproduce the warning. Thank you for showing me the precise origin of a warning that pandas has given me many times.
0

Use this:

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

2 Comments

It is a warning btw, and not an error. Is this your whole code?
I updated my question, I think its the previous command I used that's causing this. could you take a look
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.