简体   繁体   English

Pandas 将索引复制到新列

[英]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 .我正在尝试使用此处的答案将 dataframe ( newdf )的索引复制到新列( temp_index ),但我收到了臭名昭著的SettingWithCopyWarning I tried the answer here , which says to add .loc[:, colname] but it throws even more warnings.我在这里尝试了答案,它说要添加.loc[:, colname]但它会引发更多警告。 All errors are thrown on the last line of code;所有错误都在代码的最后一行抛出; no errors come up if the code stops when newdf is created.如果创建newdf时代码停止,则不会出现错误。

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.不希望重置索引,我希望dfnewdf的索引可以接受。 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\...\lib\site-packages\pandas\core\indexing.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\...\lib\site-packages\pandas\core\indexing.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

There's nothing wrong with how you are setting the temp_index column in the last line.您在最后一行设置temp_index列的方式没有任何问题。 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() .为避免此警告,请执行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.请注意,您正在使用 Boolean 密钥进行索引,AFAIK 无论如何都会创建一个副本,因此上述内容不会增加您的 memory 占用空间。 If you actually want to insert the index to df but only to a subset of the rows try如果您实际上想将索引插入df但只插入行的子集,请尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM