gpt4 book ai didi

Python Pandas 额外的逗号

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:54 25 4
gpt4 key购买 nike

我用 csv 和 pandas 做了一些工作,我必须将两个 CSV 列表合并到一个列表中并删除重复项,但最终输出在最后一列中添加了额外的逗号,我不知道为什么

我有两个这样的 CSV 列表:

  DESCRIPTION   EXTRAS   ADDRESS  AVAILABLE
1 House WiFi CP 432 1
2 Farm NONE CP 345 1
3 House Wifi CP 315 1

DESCRIPTION EXTRAS ADDRESS AVAILABLE
1 House WiFi CP 437 0
2 House Wifi CP 315 0

当我合并两者时,结果是:(“,”的数量绝对是随机的)

ID  DESCRIPTION   EXTRAS   ADDRESS  AVAILABLE,,,,,
1 House WiFi CP 432 1,,,,,,
2 Farm NONE CP 345 1,,,,
3 House Wifi CP 315 1,,,,,,
1 House WiFi CP 437 0,,,,,

这是我的代码:

with open("C:\\files\\20171412123920-1\\20171412123920-1Total.csv", "rt", encoding="utf-8") as f2:
reader = csvCSV.reader(f)
for row in reader:
merged.append(row)

with open("C:\\files\\20171412123920-1\\20171412123920-1.csv", "rt", encoding="utf-8") as f:
readerTotal = csvCSV.reader(f2)
for row in readerTotal:
merged.append(row)



with open("C:\\Users\\Desktop\\Test\\Python\\20171412123920-1Comparacion.csv", "wb") as csvfile:

spamwriter = csv.writer(csvfile,dialect='excel', encoding='utf-8')
spamwriter.writerow(["ID","DESCRIPTION","EXTRAS","ADDRESS","AVAILABLE"])
for row in merged:

spamwriter.writerow(row)

df=pd.read_csv("C:\\Users\\Desktop\\Test\\Python\\20171412123920-1Comparacion.csv", error_bad_lines=False)

df.to_string(index=False)
df.drop_duplicates(['DESCRIPTION'], keep='first', inplace = True)
df = df.reset_index(drop=True)

df.set_index('ID', inplace = True)
df.to_csv("C:\\Users\\Desktop\\Test\\Python\\201714121239201Comparacion.csv")

最佳答案

首先,您将合并 pandas 数据框中的两个 csv 文件。然后从数据框中删除重复数据。

import pandas as pd
df1=pd.read_csv('first.csv')
df2=pd.read_csv('second.csv')

frames = [df1, df2]
result=pd.concat(frames)

df5 = pd.DataFrame(result)
df5.drop_duplicates()

print(df5)

关于Python Pandas 额外的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47829588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com