I am trying to copy the index of a dataframe (newdf
) to a new column (temp_index
) using the answer here but am receiving the infamous SettingWithCopyWarning
. I tried the answer here, which says to add .loc[:, colname]
but it throws even more warnings. All errors are thrown on the last line of code; no errors come up if the code stops when newdf
is created.
What's the correct way to copy the index? Would prefer not to reset the index, I'd like the indices from df
and newdf
to be agreeable. I just need the copy column for something else.
Example Reproducible Code
col1 = [0,1,1,0,0,0,1,1,1]
col2 = [1,5,9,2,4,2,5,6,1]
df = pd.DataFrame(list(zip(col1, col2)), columns =['col1', 'col2'])
newdf = df[df.col2 >= 3]
display(df, newdf)
newdf.loc[:, 'temp_index'] = newdf.index
Errors
C:Users...libsite-packagespandascoreindexing.py:845: 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 caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self.obj[key] = _infer_fill_value(value)
C:Users...libsite-packagespandascoreindexing.py:966: 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 caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self.obj[item] = s
question from:
https://stackoverflow.com/questions/65874079/pandas-copy-index-to-new-column 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…