![](/img/trans.png)
[英]How can I remove a column from a pandas groupby if there are duplicates?
[英]Can't remove duplicates from .csv column with pandas
我正在嘗試為包含地址的.csv做一些非常簡單的事情。 我想使用pandas函數drop_duplicates()刪除任何行,如果它們在單個列中包含重復值(['Addresses'])。
每當我嘗試使用drop_duplicates()並將我的數據幀打印或保存到新的.csv時,重復的行/值仍然存在。
data = pandas.read_csv(r"C:\Users\markbrd\Desktop\PalmAveAddresses.csv",
encoding = "ISO-8859-1")
data.drop_duplicates(subset=['Addresses'], keep='first')
print(data['Addresses'])
結果:
0 4834Via Estrella
1 5244Via Patricia
2 11721HIDDEN VALLEY RD
3 30GARDEN CT
4 1999Fremont Blvd. Bldg.
5 8316Fountainhead Ct
6 8312Fountainhead Ct
7 1013Adella Ave
8 1005Adella Ave
9 1520Tenth St
10 1536Tenth St
...
607 847Florida St
608 81212th St
609 81212th St
610 81212th St
611 81212th St
612 81212th St
613 81212th St
614 81212th St
615 81212th St
616 81212th St
617 81212th St
618 81212th St
619 81212th St
如您所見,仍然有幾行在地址中包含重復項(請參閱行609-619)。 任何幫助將不勝感激!
DataFrame.drop_duplicates(subset=None, keep='first', inplace=False)
返回刪除了重復行的DataFrame,可選擇僅考慮某些列
參數: subset:列標簽或標簽序列,可選
僅考慮用於標識重復項的某些列,默認情況下使用所有列
保持:{'first','last',False},默認'first'
first:刪除第一次出現的重復項。 last:刪除重復項,除了最后一次出現。 錯誤:刪除所有重復項。 inplace:布爾值,默認為False
是否刪除重復項或返回副本
返回:
重復數據刪除:DataFrame
您需要分配或使用就地。
data.drop_duplicates(subset=['Addresses'], keep='first', inplace=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.