Mar-25-2024, 01:06 AM
So I get a dataframe, which I call 'df' into python by loading it from an excel file -
Next I create a new df, which I call 'tempdf', but using boolean indexing. tempdf is a subset of df.
So two columns in tempdf are price and size. I use a vector operation that creates a new column called weightmul which is price * size.
So is there a better way to do what I'm trying to do? Python tells me to "Try using .loc[row_indexer,col_indexer] = value instead" - but I really don't know what it means by that....
Thank you.
Next I create a new df, which I call 'tempdf', but using boolean indexing. tempdf is a subset of df.
So two columns in tempdf are price and size. I use a vector operation that creates a new column called weightmul which is price * size.
tempdf['weightmul'] = tempdf.apply(lambda row: row['price'] * row['size'], axis = 1)So python doesn't like this. I gives me this warning:
Error: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
Now, my vector operation does exactly what I want - it creates a new column called weightmul, which is the product of price * size. Now, if I run this operation on the original dataframe, df, it's happy. But if it's a subset dataframe, tempdf which was created using indexing, I get the warning. So is there a better way to do what I'm trying to do? Python tells me to "Try using .loc[row_indexer,col_indexer] = value instead" - but I really don't know what it means by that....
Thank you.