pandas drop_duplicates 有效,但使用 .to_csv 保存时它仍然显示所有
我只是想从 csv 中删除重复项,然后创建一个仅包含第一列且没有重复项的新 csv 文件。
我的终端显示其工作状态,但新的 csv 文件仍然显示所有内容。 ???
import pandas as pd
import numpy as np
#df = pd.read_csv('directory.csv',index_col=0,usecols=["From"]),
d = pd.read_csv('directory.csv')
df = pd.DataFrame(d, columns=['From'])
print(
"""
-----this is all phone numbers in header FROM-----
"""
)
print(df)
print(
"""
-----this is only unique values ----
"""
)
df = df.drop_duplicates(subset="From", keep="first", inplace=True)
print(df)
print(
"""
-----now saving to new csv----
"""
)
df.to_csv("uniquePhones.csv")
终端 python3 csvImport.py
-----这是标头中的所有电话号码 FROM-----
From
0 +34141414)
1 1231231231
2 1231213
3 (+123123123
4 123212313.. ...
692 1231237)
693 A123213616)
694 12321433)
695 1312)
696 1321321)
[697 rows x 1 columns]
-----这只是唯一值 ----
From
0 +34141414)
1 1231231231
2 1231213
3 (+123123123
4 123212313..
692 1231237)
693 A123213616)
694 12321433)
695 1312)
696 1321321)
[279 rows x 1 columns]
-----现在保存到新的 csv----
I'm simply trying to remove duplicates from a csv and then make a new csv file with only the first column and no duplicates.
My terminal shows its working but when then the new csv file still shows all. ???
import pandas as pd
import numpy as np
#df = pd.read_csv('directory.csv',index_col=0,usecols=["From"]),
d = pd.read_csv('directory.csv')
df = pd.DataFrame(d, columns=['From'])
print(
"""
-----this is all phone numbers in header FROM-----
"""
)
print(df)
print(
"""
-----this is only unique values ----
"""
)
df = df.drop_duplicates(subset="From", keep="first", inplace=True)
print(df)
print(
"""
-----now saving to new csv----
"""
)
df.to_csv("uniquePhones.csv")
Terminal
python3 csvImport.py
-----this is all phone numbers in header FROM-----
From
0 +34141414)
1 1231231231
2 1231213
3 (+123123123
4 123212313.. ...
692 1231237)
693 A123213616)
694 12321433)
695 1312)
696 1321321)
[697 rows x 1 columns]
-----this is only unique values ----
From
0 +34141414)
1 1231231231
2 1231213
3 (+123123123
4 123212313..
692 1231237)
693 A123213616)
694 12321433)
695 1312)
696 1321321)
[279 rows x 1 columns]
-----now saving to new csv----
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遇到了同样的错误,通过执行以下操作修复了它:
Had the same error, fixed it by doing: