I have derived a dataframe using groupby.
I am trying to set a new column ‘rank’ based on the ‘volume’ column using the below code.
The code is working but giving a warning –
JavaScript
x
3
1
A value is trying to be set on a copy of a slice from a DataFrame.
2
Try using .loc[row_indexer,col_indexer] = value instead
3
Below is my code. Would appreciate guidance as I am not too much experienced in Python/ Pandas.
JavaScript
1
6
1
filter_condition = ohlcv_m30_groupby_df['min_volume'] > cutoff_volume
2
min_100k_volume_df = ohlcv_m30_groupby_df[filter_condition]
3
# min_100k_volume_df.insert(len(min_100k_volume_df.columns), 'volume_rank', pd.Series([], dtype='int8'))
4
min_100k_volume_df.loc[:,'volume_rank'] = min_100k_volume_df['total_volume'].rank(ascending = False).astype(int)
5
print(min_100k_volume_df)
6
Advertisement
Answer
Because after you filter your dataframe and assign to another variable name even if you use loc you are still using a shallow copy. In order to not get the warning you should copy the dataframe properly like this.
JavaScript
1
3
1
min_100k_volume_df = ohlcv_m30_groupby_df[filter_condition].copy()
2
3