Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python 3.8 - Pandas copy index to new column

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

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's nothing wrong with how you are setting the temp_index column in the last line. The issue is as it says in the warning. What are you actually trying to achieve? To avoid this warning do newdf = df[df.col2 >= 3].copy(). Note you are indexing with a Boolean key which, AFAIK, creates a copy anyway so the above will not increase your memory footprint. If you actually want to insert the index to df but only to a subset of the rows try

key = df.col2 >= 3
df = df.loc[key, 'temp_index'] = df.index[key]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...