SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

问题描述:XXX.py:17: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

关键报错代码:

lanelineCSV = "LaneObject.csv"
lanelinePD = pd.read_csv(lanelineCSV,low_memory=False)
lanelinePD = lanelinePD.drop(lanelinePD.columns[0],axis=1)

print("read csv end")
# drop "#" form value
for i in lanelinePD.index:
    for c in lanelinePD.columns:
        if type(lanelinePD.loc[i][c]) is str:
            lanelinePD.loc[i][c] = lanelinePD.loc[i][c].replace("#", "")

报错原因: lanelinePD.loc[i][c] 是标签引用,不能作为赋值使用

更新为: lanelinePD.loc[i,c] 即可

print("read csv end")
# drop "#" form value
for i in lanelinePD.index:
    for c in lanelinePD.columns:
        if type(lanelinePD.loc[i][c]) is str:
            lanelinePD.loc[i,c] = lanelinePD.loc[i][c].replace("#", "")

print("drop # end ")

你可能感兴趣的:(问题杂记,python,开发语言,pandas)