python - 无法使用熊猫从 .csv 列中删除重复项
问题描述
我正在尝试对包含地址的 .csv 做一些非常简单的事情。如果它们在单个列(['Addresses'])中包含重复值,我想使用 pandas 函数 drop_duplicates() 删除任何行。
每当我尝试使用 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 行)。任何帮助将不胜感激!
解决方案
您需要就地分配或使用。
data.drop_duplicates(subset=['Addresses'], keep='first', inplace=True)
推荐阅读
- python - 从 Python 中用括号括起来的字符串中删除唯一字符
- kubernetes - Kubernetes 手动备份
- c# - 如何在值之后仅获取数组最接近的值
- sql-server - SQL Server:获取出现 ProductID 的所有行
- bash - 我的 $PATH 中有未在 .bashrc 文件中列出的可执行文件。他们来自哪里?
- python - 删除 python pandas 列中的特定字符串
- javascript - 无法设置 Laravel+VueJS 向我的模板呈现任何内容
- json - 如何为 JSON 提出对 RFC(s) 的一些修订以考虑评论?
- embedded - 连接 16*2 键盘和 LCD 与 atmega32
- angular - 使用 router.config.unshift() 设置动态路由仅在从 `AppComponent app.component.ts` 使用时才有效