Pandas concat: ValueError: Shape of passed values is blah, indices imply blah2

前端 未结 7 984
梦如初夏
梦如初夏 2020-11-27 16:41

I\'m trying to merge a (Pandas 14.1) dataframe and a series. The series should form a new column, with some NAs (since the index values of the series are a subset of the ind

7条回答
  •  Happy的楠姐
    2020-11-27 17:26

    To drop duplicate indices, use df = df.loc[df.index.drop_duplicates()]. C.f. pandas.pydata.org/pandas-docs/stable/generated/… – BallpointBen Apr 18 at 15:25

    This is wrong but I can't reply directly to BallpointBen's comment due to low reputation. The reason its wrong is that df.index.drop_duplicates() returns a list of unique indices, but when you index back into the dataframe using those the unique indices it still returns all records. I think this is likely because indexing using one of the duplicated indices will return all instances of the index.

    Instead, use df.index.duplicated(), which returns a boolean list (add the ~ to get the not-duplicated records):

    df = df.loc[~df.index.duplicated()]
    

提交回复
热议问题