0

I am trying to apply a .str[] to a pandas data from so I can get specific characters from a string but I get a "SettingWithCopyWarning" error.

import pandas as pd

df_temp = pd.read_csv("C:\\Users\\Bill-PC\\PycharmProjects\\QC Data\\Testing\\10 PP 1 2 again.txt", sep="\t", low_memory=False)

df_temp = df_initial.loc[df_initial["Sample Type"] == "Quality Control"]

df_temp["QC Level"] = df_temp["Sample Name"].str[-1:]

I get the following error

<input>:3: 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
  • The problem is your second line returns a view, which you then modify in the third line. You can add `.copy()` to the end of your second line to guarantee `df_temp` is it's own object. – ALollz Jul 17 '19 at 15:44

1 Answers1

0

By the way this is not an error it is just a warning but try this I hope warning would not be shown

df.loc[:,"City"] = df['City'].str[-1:]
tawab_shakeel
  • 3,701
  • 10
  • 26