In python pandas SettingWithCopyWarning 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

Purpose

Prevent the following warnings from appearing! (The code that gives this warning is shown below)

aa.py:5: SettingWithCopyWarning:
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

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  d['col3']= d['col2'] * 3
   col1  col2  col3
0     1     2     6
2     1     2     6

For the time being, take a look at the written link

http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

dfmi.loc[:,('one','second')] = value
# becomes
dfmi.loc.__setitem__((slice(None), ('one', 'second')), value)

It is written.

Code in question

aa.py


import pandas as pd

data = pd.DataFrame([[1,2],[2,3],[1,2],[2,3]], columns=['col1','col2'])
d = data[data['col1'] == 1]
d['col3']= d['col2'] * 3
print(d)

If you do this, you will get the above Warning.

The problem lies in the following line.

d = data[data['col1'] == 1]

If this is d = data, the above Warning is not available.

Cause & Solution 1

Since data has values other than col1 == 1, if you try to add a new Column to the DF that has been cut out by col1 == 1, it will be strange, so make it a copy and only col1 == 1 in advance Just make a DF.

Revised


d = data[data['aa'] == 2].copy()

Cause & Solution 2

As supported in the link above, use .loc and update only the corresponding part.

Revised


#Put Value (right side) in col3 (new Column) although col1 meets the condition of 1.
data.loc[data['col1'] == 1, 'col3'] = data['col2'] * 3
#Extract only the part where col1 is 1 from data
d = data[data['col1'] == 1]
print('data:\n %s' % data)
print('d:\n%s' % d)

result


data:
    col1  col2  col3
0     1     2     6
1     2     3   NaN
2     1     2     6
3     2     3   NaN
d:
   col1  col2  col3
0     1     2     6
2     1     2     6

Recommended Posts

In python pandas SettingWithCopyWarning 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
Examples and countermeasures for "A value is trying to be set on a copy of a slice from a Data Frame." Warning in pandas
How to get a value from a parameter store in lambda (using python)
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
How to find out if there is an arbitrary value in "somewhere" of pandas DataFrame
How to create an instance of a particular class from dict using __new__ () in python
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
How to slice a block multiple array from a multiple array in Python
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
Try to get a list of breaking news threads in Python.
How to find the memory address of a Pandas dataframe value
Try to log in to Netflix automatically using python on your PC
DataFrame of pandas From creating a DataFrame from two lists to writing a file
[Python] What is a formal argument? How to set the initial value
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
A memorandum because I stumbled on trying to use MeCab in Python
Try to solve a set problem of high school math with Python
Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.