Remove rows with duplicate indices (Pandas DataFrame and TimeSeries)

后端 未结 6 915
我寻月下人不归
我寻月下人不归 2020-11-22 14:31

I\'m reading some automated weather data from the web. The observations occur every 5 minutes and are compiled into monthly files for each weather station. Once I\'m done pa

6条回答
  •  悲&欢浪女
    2020-11-22 15:14

    This adds the index as a dataframe column, drops duplicates on that, then removes the new column:

    df = df.reset_index().drop_duplicates(subset='index', keep='last').set_index('index').sort_index()
    

    Note that the use of .sort_index() above at the end is as needed and is optional.

提交回复
热议问题