简体   繁体   中英

Data Analysis Pandas SettingWithCopyWarning

 numbers = LabelEncoder()
 State_Data['Quality'] = numbers.fit_transform(State_Data['Quality 
 Parameter'].astype('str'))

 this is generating the following error :

 /opt/conda/lib/python3.5/site-packages/ipykernel/__main__.py: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

I'm getting this error while doing analysis on a water quality data set and couldn't resolve it.It suggests to use .loc but i've tried that too and it didn't work either. Please tell me how to resolve this issue?

It's always better to use a different variable to store transformed data. You can try following code to avoid the warnings:

transformed = numbers.fit_transform(State_Data['Quality Parameter'].astype('str'))

State_Data['Quality_transformed'] = transformed

Using this approach, will not only help you to avoid warnings but also will not modify your actual raw data, modifying raw data sometimes becomes problematic as you might have to run the entire script again if anything goes wrong along the way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM