在熊猫上使用.str时的SettingWithCopyWarning

时间:2019-07-17 15:32:20

标签: python pandas

我正在尝试将.str []应用于来自的熊猫数据,以便可以从字符串中获取特定字符,但出现“ SettingWithCopyWarning”错误。

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:]

我收到以下错误

<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

1 个答案:

答案 0 :(得分:0)

通过这种方式,这不是一个错误,它只是一个警告,但请尝试执行此操作,希望不会显示警告

df.loc[:,"City"] = df['City'].str[-1:]