pandas drop_duplicates 有效,但使用 .to_csv 保存时它仍然显示所有

发布于 2025-01-18 08:00:31 字数 1318 浏览 2 评论 0原文

我只是想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你曾走过我的故事 2025-01-25 08:00:31

遇到了同样的错误,通过执行以下操作修复了它:

df = df.drop_duplicates().reset_index()
df.to_csv() # Now works

Had the same error, fixed it by doing:

df = df.drop_duplicates().reset_index()
df.to_csv() # Now works
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文