I’m trying to add a new column to my DataFrame Transactions_Meat
:
Transactions_Meat['Avg_IPrice'] = Transactions_Meat['Rev'] / Transactions_Meat['Units_Sold']
Resulting to this warning:
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
Transactions_Meat
is a copy of a DataFrame named Transactions
:
Transactions_Meat = Transactions[Transactions['Category'] == 'Meat']
So, im trying to use .loc
which changed my approach to:
Transactions_Meat.loc[:,'Avg_IPrice'] = Transactions_Meat['Rev'] / Transactions_Meat['Units_Sold']
Unfortunately, I’m still getting the same warning. I've visited the documentation of pandas as described here. I also checked the stackoverflow-question which is already handling this problem. But I couldn’t get rid of the warning.
Any possbility to remove the warning using the DataFrame.loc method?