首页 > 解决方案 > 无法在 python DataFrame 中的索引中删除重复项

问题描述

我正在尝试删除多索引 python 中的重复索引pandas dataframe

    import pandas as pd
    arrays=[['i1', 'i2', 'i3'], ['j1', 'j2', 'j3']]
    tuples = list(zip(*arrays))
    index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
    df=pd.DataFrame({'a':['b', 'c', 'd']}, index=index)
    df = df.groupby(df.index).first()
    print(df)

但索引中的重复项尚未删除:

          a
(i1, j1)  b
(i2, j2)  c
(i3, j3)  d

我怎么做?

预期输出:

          a
(i1, j1)  b
(i2, j2)  c

标签: pythonpandasmulti-index

解决方案


推荐阅读