Skip to content
Advertisement

Warning while adding rank column to a pandas dataframe

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 –

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

Below is my code. Would appreciate guidance as I am not too much experienced in Python/ Pandas.

filter_condition = ohlcv_m30_groupby_df['min_volume'] > cutoff_volume
min_100k_volume_df = ohlcv_m30_groupby_df[filter_condition]
# min_100k_volume_df.insert(len(min_100k_volume_df.columns), 'volume_rank', pd.Series([], dtype='int8'))
min_100k_volume_df.loc[:,'volume_rank'] = min_100k_volume_df['total_volume'].rank(ascending = False).astype(int)
print(min_100k_volume_df)

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.

min_100k_volume_df = ohlcv_m30_groupby_df[filter_condition].copy()

Advertisement