Pandas:更改重复项的索引

huangapple go评论82阅读模式
英文:

Pandas : change the index of the duplicates

问题

我有2个DataFrame:df0df1,以及df1.shape[0] > df1.shape[0]

df0df1具有完全相同的列。
df0的大多数行都在df1中。

df0df1的索引是

df0.index = range(df0.shape[0])
df1.index = range(df1.shape[0])

然后我创建了dft

dft = pd.concat([df0, df1], axis=0, sort=False)

并使用以下代码删除了重复的行

dft.drop_duplicates(subset='this_col_is_not_index', keep='first', inplace=True)

dft的索引上我有一些重复的行。例如:

dft.loc3.shape
返回

(2, 38)

我的目标是将返回的第二行的索引更改为具有唯一索引3
这第二行的索引应该是dft.index.sort_values()[-1]+1

我想在所有重复的行上应用这个操作。

参考链接:

Python Pandas: 获取某列匹配特定值的行的索引

Pandas:获取重复的索引

重新定义Pandas DataFrame对象的索引

英文:

I have 2 DataFrames : df0 and df1 and df1.shape[0] > df1.shape[0].

df0 and df1 have the exact same columns.
Most of the rows of df0 are in df1.

The indices of df0 and df1 are

df0.index = range(df0.shape[0])
df1.index = range(df1.shape[0])

I then created dft

dft = pd.concat([df0, df1], axis=0, sort=False)

and removed duplicated rows with

dft.drop_duplicates(subset='this_col_is_not_index', keep='first', inplace=True)

I have some duplicates on the index of dft. For example :

dft.loc[3].shape

returns

(2, 38)

My aim is to change the index of the second row returned to have a unique index 3.
This second row should be indexed dft.index.sort_values()[-1]+1.

I would like to apply this operation on all duplicates.

References :

Python Pandas: Get index of rows which column matches certain value

Pandas: Get duplicated indexes

Redefining the Index in a Pandas DataFrame object

答案1

得分: 2

concat中添加参数ignore_index=True以避免重复的索引值:

dft = pd.concat([df0, df1], axis=0, sort=False, ignore_index=True)
英文:

Add parameter ignore_index=True to concat for avoid duplicated index values:

dft = pd.concat([df0, df1], axis=0, sort=False, ignore_index=True)

答案2

得分: 1

Use reset_index(drop=True)

dft.reset_index(drop=True)
英文:

Use reset_index(drop = True)

dft.reset_index(drop=True)

huangapple
  • 本文由 发表于 2020年1月3日 18:10:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定