-1

I want to make a column of a data frame of pandas, the second row to the last row equal to another column's first row to the second last row: it look like:

r(t-1).iloc[1:] = r(t).iloc[0:-1](r(t) and r(t-1)) 

are columns of the same data frame

the problem I met is python wouldn't get my idea that I want to shift of row of copying data :

IR['r(t-1)'].iloc[1:] = IR['r(t)'].iloc[0:-1]
/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:190: 

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

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

  self._setitem_with_indexer(indexer, value)

any one know how to deal with it?

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
panda
  • 3
  • 2
  • 3
    Welcome to SO, can you provide [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) ? – shaik moeed Mar 09 '19 at 04:28
  • 1
    Also check [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – anky Mar 09 '19 at 04:41
  • About Asking questions: As other guys also indicate, writing a good informative example would be great to help other people understand your problem, and also for other people having the same problem, to recognize that this is their question too. Even if you have found the answer, please take your time, formatting your question by putting your warning message in a code block, and include an informative pandas example which can reproduce the same error you have ran into – Alireza Mar 09 '19 at 11:15

1 Answers1

0

Welcome to SO.

How to solve your problem

It seems you need a shift in one of your columns. as the first two lines of your question indicate. you can do it by shift() method for pandas dataframes. Then your answer could possibly would be:

df['new_row'] = df['old_row'].shift(1)

You can shift forward or backward with negative and positive values of shifting

What is the error you have ran into

Briefly speaking, pandas has provided ways of data frame writing and reading, and it does not actually accept any other kind of doing that. The warning shows that you are not using a good way (in pandas humble opinion!) of writing to the dataframe (using iloc) as iloc is mostly used for accessing to rows by index.

Any pandas expert here, please correct me if I am wrong.

Alireza
  • 656
  • 1
  • 6
  • 20