By ba_ul

2014-11-03 22:23:20 8 Comments

Python 3.4 and Pandas 0.15.0

df is a dataframe and col1 is a column. With the code below, I'm checking for the presence of the value 10 and replacing such values with 1000.

df.col1[df.col1 == 10] = 1000

Here's another example. This time, I'm changing values in col2 based on index.

df.col2[df.index == 151] = 500

Both these produce the warning below:

-c:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the the caveats in the documentation:


cols = ['col1', 'col2', 'col3']
df[cols] = df[cols].applymap(some_function)

This produces a similar warning, with an added suggestion:

Try using .loc[row_indexer,col_indexer] = value instead

I'm not sure I understand the discussion pointed to in the warnings. What would be a better way to write these three lines of code?

Note that the operations worked.


@koelemay 2014-11-04 16:19:03

Agreed with Paul about 'loc' usage.

For your applymap case you should be able to do this:

cols = ['col1', 'col2', 'col3']
df.loc[:, cols] = df[cols].applymap(some_function)

@ba_ul 2014-11-05 02:31:56

That gives the same warning. But this one doesn't: df.loc[:, cols] = df.loc[:, cols].applymap(some_function)

@koelemay 2014-11-05 15:15:29

Interesting. I'm using pandas 0.15.0 but Python 2.7.5 so didn't test with your environment. My suggestion does not throw the warning for me. Good to know that your latter attempt worked

@Tom 2018-12-24 16:17:09

I'm getting the copy warning even though I'm using .loc: col_pct = ['Cash', 'Funds'] ui_df.loc[:, col_pct] = ui_df.loc[:, col_pct].applymap('{0:.2f}%'.format)

@Paul H 2014-11-03 22:37:48

The issue here is that: df.col1[df.col1 == 10] returns a copy.

So I would say:

row_index = df.col1 == 10
# then with the form .loc[row_indexer,col_indexer]
df.loc[row_index, 'col1'] = 100

@ba_ul 2014-11-04 03:16:36

Thanks. Should be df.loc[row_index, 'col1'] = 100, right?

@Paul H 2014-11-04 03:18:53

@asif.m are are of course 100% correct. I'll fix it

@ba_ul 2014-11-04 03:48:17

What do you suggest I do for the third example (with "applymap")?

@Paul H 2014-11-04 16:18:19

@asif.m I need a runnable example that uses applymap to assist further.

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] Renaming columns in pandas

24 Answered Questions

[SOLVED] Adding new column to existing DataFrame in Python pandas

22 Answered Questions

[SOLVED] How to iterate over rows in a DataFrame in Pandas

15 Answered Questions

[SOLVED] How to deal with SettingWithCopyWarning in Pandas?

1 Answered Questions

[SOLVED] Correct way to set value in multi-index Pandas Dataframe

1 Answered Questions

[SOLVED] Convert a column from pandas dataframe from float to int

  • 2017-09-27 03:13:09
  • niukasu
  • 7761 View
  • 3 Score
  • 1 Answer
  • Tags:   python pandas

2 Answered Questions

[SOLVED] Pandas: SettingWithCopyWarning:

  • 2017-07-18 14:55:37
  • salhin
  • 1723 View
  • 6 Score
  • 2 Answer
  • Tags:   python pandas

1 Answered Questions

SettingWithCopyWarning Python changing column datatype in Dataframe

Sponsored Content