我正在使用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")
答案 0 :(得分:1)
首先,您将合并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)