正确访问存在重复索引值的切片 [英] correct accessing of slices with duplicate index-values present

查看:36
本文介绍了正确访问存在重复索引值的切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有索引的数据框,该索引有时包含具有相同索引值的行.现在,我想对数据框进行切片,并根据行索引设置值.

I have a dataframe with an index that sometimes contains rows with the same index-value. Now I want to slice that dataframe and set values based on row-indices.

请考虑以下示例:

import pandas as pd

df = pd.DataFrame({'index':[1,2,2,3], 'values':[10,20,30,40]})
df.set_index(['index'], inplace=True)

df1 = df.copy()
df2 = df.copy()

#copy warning
df1.iloc[0:2]['values'] = 99
print(df1)

df2.loc[df.index[0:2], 'values'] = 99
print(df2)

df1是预期的结果,但是给了我一个SettingWithCopyWarning.df2似乎是建议的文档访问方式,但是给我错误的结果(由于重复的索引)

df1 is the expected result, but gives me a SettingWithCopyWarning. df2 seems to be the suggested way of accessing by the doc, but gives me the wrong result (because of the duplicate index)

是否存在一种正确的"方法来正确设置那些存在重复索引值的值?

Is there a "proper" way to set those values correctly with the duplicate index-values present?

推荐答案

.loc .因此,您必须进行基于位置的选择 iloc .由于我们需要通过职位,因此我们必须使用 get_loc 用于获取列的位置:

.loc is not recommended when you have duplicate index. So you have to go for position based selection iloc. Since we need to pass the positions, we have to use get_loc for getting position of column:

print (df2.columns.get_loc('values'))
0

df1.iloc[0:2, df2.columns.get_loc('values')] = 99
print(df1)
       values
index        
1          99
2          99
2          30
3          40

这篇关于正确访问存在重复索引值的切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆