cannot transform values in pandas dataframe using a mask

Here is an example to illustrate. I am doing something as follows:

import numpy as np
import pandas as pd

data = {'col_1': [3, 5, -1, 0], 'col_2': ['a', 'b', 'c', 'd']}

x = pd.DataFrame.from_dict(data)

mask = x['col_1'].values > 0

x[mask]['col_1'] = np.log(x[mask]['col_1'])

This comes back with:

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

Also, the dataframe remains unchanged.

>Solution :

Use DataFrame.loc for select and set column with condition:

mask = x['col_1'].values > 0

x.loc[mask, 'col_1'] = np.log(x.loc[mask, 'col_1'])
print (x)
      col_1 col_2
0  1.098612     a
1  1.609438     b
2 -1.000000     c
3  0.000000     d

Leave a Reply