刪除具有重複索引的熊貓行 - Remove pandas rows with duplicate indices

問題:

How to remove rows with duplicate index values?如何刪除具有重複索引值的行?

In the weather DataFrame below, sometimes a scientist goes back and corrects observations -- not by editing the erroneous rows, but by appending a duplicate row to the end of a file.在下面的天氣數據框中,有時科學家會返回並更正觀察結果——不是通過編輯錯誤的行,而是通過在文件末尾附加重複的行。

I'm reading some automated weather data from the web (observations occur every 5 minutes, and compiled into monthly files for each weather station.) After parsing a file, the DataFrame looks like:我正在從網上讀取一些自動天氣數據(每 5 分鐘進行一次觀測,並編譯成每個氣象站的月度文件。)解析文件後,DataFrame 如下所示:

                      Sta  Precip1hr  Precip5min  Temp  DewPnt  WindSpd  WindDir  AtmPress
Date                                                                                      
2001-01-01 00:00:00  KPDX          0           0     4       3        0        0     30.31
2001-01-01 00:05:00  KPDX          0           0     4       3        0        0     30.30
2001-01-01 00:10:00  KPDX          0           0     4       3        4       80     30.30
2001-01-01 00:15:00  KPDX          0           0     3       2        5       90     30.30
2001-01-01 00:20:00  KPDX          0           0     3       2       10      110     30.28

Example of a duplicate case:重複案例的示例:

import pandas 
import datetime

startdate = datetime.datetime(2001, 1, 1, 0, 0)
enddate = datetime.datetime(2001, 1, 1, 5, 0)
index = pandas.DatetimeIndex(start=startdate, end=enddate, freq='H')
data1 = {'A' : range(6), 'B' : range(6)}
data2 = {'A' : [20, -30, 40], 'B' : [-50, 60, -70]}
df1 = pandas.DataFrame(data=data1, index=index)
df2 = pandas.DataFrame(data=data2, index=index[:3])
df3 = df2.append(df1)

df3
                       A   B
2001-01-01 00:00:00   20 -50
2001-01-01 01:00:00  -30  60
2001-01-01 02:00:00   40 -70
2001-01-01 03:00:00    3   3
2001-01-01 04:00:00    4   4
2001-01-01 05:00:00    5   5
2001-01-01 00:00:00    0   0
2001-01-01 01:00:00    1   1
2001-01-01 02:00:00    2   2

And so I need df3 to eventually become:所以我需要df3最終變成:

                       A   B
2001-01-01 00:00:00    0   0
2001-01-01 01:00:00    1   1
2001-01-01 02:00:00    2   2
2001-01-01 03:00:00    3   3
2001-01-01 04:00:00    4   4
2001-01-01 05:00:00    5   5

I thought that adding a column of row numbers ( df3['rownum'] = range(df3.shape[0]) ) would help me select the bottom-most row for any value of the DatetimeIndex , but I am stuck on figuring out the group_by or pivot (or ???) statements to make that work.我認爲添加一列行號( df3['rownum'] = range(df3.shape[0]) )將幫助我爲DatetimeIndex任何值選擇最底部的行,但我一直在弄清楚group_bypivot (或???)語句來完成這項工作。


解決方案:

參考一: https://stackoom.com/question/shCG
參考二: Remove pandas rows with duplicate indices
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章