Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading