我想制作比較兩個csv檔案的代碼!
import pandas as pd
import numpy as np
df = pd.read_csv("E:\Dupfile.csv")
df1 = pd.read_csv("E:\file.csv")
df['Correct'] = None
def Result(x):
if ....:
return int(1)
else:
return int(0)
df.loc[:,"Correct"]=df.apply(Result,axis=1)
print(df["Correct"])
df.to_csv("E:\file.csv")
print(df.head(20))
例如,file.csv 格式如下所示:
round date first second third fourth fifth sixth
0 1 2021.04 1 14 15 24 40 41
1 2 2021.04 2 9 10 16 35 37
2 3 2021.04 4 15 24 35 36 40
3 4 2021.03 10 11 20 21 25 41
4 5 2021.03 4 9 23 26 29 33
5 6 2021.03 1 9 26 28 30 41
Dupfile.csv 如下所示:
round date first second third fourth fifth sixth
0 1 2021.04 1 14 15 24 40 41
0 1 2021.04 1 2 3 4 5 6
1 2 2021.04 2 9 10 16 35 37
1 2 2021.04 1 2 3 4 5 6
2 3 2021.04 4 15 24 35 36 40
2 3 2021.04 1 2 3 4 5 6
3 4 2021.03 10 11 20 21 25 41
3 4 2021.03 1 2 3 4 5 6
4 5 2021.03 4 9 23 26 29 33
4 5 2021.03 1 2 3 4 5 6
它還有一個相同的回合,但價值不同。
使用 Dupfile 的輪次檢查檔案的輪次值,如果第一個到第六個值相等,則在 Dupfile 中創建另一個“正確”列并放入 1。如果不正確,將 0 放入“正確”列。
我試圖比較兩個不同的 csv 檔案,但我不知道該怎么做。有人能幫我嗎?
我的期望答案:
round date first second third fourth fifth sixth Correct
0 1 2021.04 1 14 15 24 40 41 1
0 1 2021.04 1 2 3 4 5 6 0
1 2 2021.04 2 9 10 16 35 37 1
1 2 2021.04 1 2 3 4 5 6 0
2 3 2021.04 4 15 24 35 36 40 1
2 3 2021.04 1 2 3 4 5 6 0
3 4 2021.03 10 11 20 21 25 41 1
3 4 2021.03 1 2 3 4 5 6 0
4 5 2021.03 4 9 23 26 29 33 1
4 5 2021.03 1 2 3 4 5 6 0
uj5u.com熱心網友回復:
如果使用pandas模塊,最好獲取模塊中提供的方法。我建議您,嘗試merge用于比較 2 個不同的 DataFrame。我將您的代碼重寫如下。
import pandas as pd
df = pd.read_csv("E:\Dupfile.csv")
df1 = pd.read_csv("E:\file.csv")
df1['Correct'] = 1
df = df.merge(
df1,
how='left',
on=['round',
'date',
'first',
'second',
'third',
'fourth',
'fifth',
'sixth']).fillna(0)
print(df)
print(df['Correct'])
df.to_csv("E:\file.csv")
print(df.head(20))
它是如何作業的?
該merge方法試圖在匹配的列df和df1與存在于相同的名稱on的陣列。當您選擇left的how說法,在合并的左側(沒有值df)將被洗掉(左連接)。在另一種方式中,correct我們創建的列file.csv附加到Dupfil.csv資料,并且不匹配的被分配為nan值。該fillna(0)方法幫助我們將nan值替換為 0。
pandas.DataFrame.merge API 參考
uj5u.com熱心網友回復:
您可以使用純熊貓來做到這一點df.merge。
查看示例:
import pandas as pd
# file.csv
file_df = pd.DataFrame(
columns=["round", "date", "first", "second", "third", "fourth", "fifth", "sixth"],
data=[
("1", "2021.04", "1", "14", "15", "24", "40", "41"),
("2", "2021.04", "2", "9", "10", "16", "35", "37"),
("3", "2021.04", "4", "15", "24", "35", "36", "40"),
("4", "2021.03", "10", "11", "20", "21", "25", "41"),
("5", "2021.03", "4", "9", "23", "26", "29", "33"),
("6", "2021.03", "1", "9", "26", "28", "30", "41"),
],
)
# adding control column (we already know that those are the right values)
file_df["correct"] = 1
# Dupfile.csv
dup_file_df = pd.DataFrame(
columns=["round", "date", "first", "second", "third", "fourth", "fifth", "sixth"],
data=[
("1", "2021.04", "1", "14", "15", "24", "40", "41"),
("1", "2021.04", "1", "2", "3", "4", "5", "6"),
("2", "2021.04", "2", "9", "10", "16", "35", "37"),
("2", "2021.04", "1", "2", "3", "4", "5", "6"),
("3", "2021.04", "4", "15", "24", "35", "36", "40"),
("3", "2021.04", "1", "2", "3", "4", "5", "6"),
("4", "2021.03", "10", "11", "20", "21", "25", "41"),
("4", "2021.03", "1", "2", "3", "4", "5", "6"),
("5", "2021.03", "4", "9", "23", "26", "29", "33"),
("5", "2021.03", "1", "2", "3", "4", "5", "6"),
],
)
# We extract the column names to use in the merging process
cols = [x for x in dup_file_df.columns]
# We merge the 2 dataframes.
# The data frames are to match on every column (round, date and first to sixth).
# The "correct" column will be populated only if all the columns are matching
merged = dup_file_df.merge(file_df, how="outer", left_on=cols, right_on=cols)
# We put "0" where correct is None and cast to integer (it was float)
merged["correct"] = merged["correct"].fillna(0).astype(int)
# Done!
print(merged)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/321809.html
上一篇:python生成的csv中的空行
