'Replace entire pandas dataframe after scaling without warning
I have tried this according to this awnser
x = df[feature_collums]
y = df[[label_column]][label_column]
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
x[:] = scaler.fit_transform(x, y)
print('Scaled the data to the 0-1 interval')
But this gives me warning:
/tmp/ipykernel_560/2431060981.py:14: 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
I have a hard time converting this code to using the .loc atribute. Could someone please show me how to convert this code to using .loc and getting rid of the warning?
Thank you!
Solution 1:[1]
Ok I found here that you can use this:
data_scaled = pd.DataFrame(scaled_features, index=df.index, columns=df.columns)
Basically just creating a new dataframe with the same index and column and this gets rid of the warning.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Quinten Cabo |