所以我在這里和其他網站上閱讀了幾個主題,但我仍然不知道該怎么做。它看起來像一個簡單的腳本,但我已經被困了幾天了。
本質上,我得到了兩個輸入檔案(假設是 CSV 檔案)。每個檔案有 2 列,id 和 col_name。兩個檔案之間的區別在于 col_name 的命名約定。一個檔案是小寫的,另一個是camelCase。
我想知道如何通過 col_name 匹配這兩個檔案,然后創建一個包含 4 列的輸出檔案,id、col_name、col_name、id。
輸入1.csv
1 _id
2 rawrequest
3 rawresponse
4 products
5 _deleted
6 enterpriseid
7 source
8 transactionuid
9 type
10 isotransactiontype
11 status
12 terminalid
13 merchantid
14 merchantname
15 settlementbatchid
16 errordescription
17 referencetransactions
18 createdat
19 updatedat
20 __v
輸入2.csv
101 _id
102 rawRequest
103 rawResponse
104 products
105 _deleted
106 enterpriseid
107 source
108 transactionUid
109 type
110 isoTransactionType
111 status
112 terminalId
113 merchantId
114 merchantName
115 settlementBatchId
116 errorDescription
117 referenceTransactions
118 createdAt
119 updatedAt
120 __v
期望的輸出:
1 _id _id 101
2 rawrequest rawRequest 102
3 rawresponse rawResponse 103
4 products products 104
5 _deleted _deleted 105
6 enterpriseid enterpriseid 106
7 source source 107
8 transactionuid transactionUid 108
9 type type 109
10 isotransactiontype isoTransactionType 110
11 status status 111
12 terminalid terminalId 112
13 merchantid merchantId 113
14 merchantname merchantName 114
15 settlementbatchid settlementBatchId 115
16 errordescription errorDescription 116
17 referencetransactions referenceTransactions 117
18 createdat createdAt 118
19 updatedat updatedAt 119
20 __v __v 120
我嘗試撰寫的代碼:
import pandas as pd
csv1 = pd.read_csv("Input1.csv")
csv2 = pd.read_csv("Input2.csv")
# Method 1
merge_data = csv1.merge(csv2, on = 'col_name')
merge_data.to_csv("output.csv", index = False)
# Method 2
merge = pd.merge(csv1, csv2, how="outer")
merge.to_csv("output1.csv", index = False)
# Method 3
import csv
with open('Input1.csv', 'r') as csv_file1:
csv_reader = csv.reader(csv_file1)
with open('output2.csv', 'w') as new_file:
csv_writer = csv.writer(new_file)
for line in csv_reader:
csv_writer.writerow(line)
with open('Input2.csv', 'r') as csv_file2:
csv_reader2 = csv.reader(csv_file2)
with open('output2.csv', 'a') as new_file:
csv_writer = csv.writer(new_file)
for line in csv_reader2:
csv_writer.writerow(line)
輸出(來自代碼):
輸出.csv
id_x,col_name,id_y
1,_id,101
4,products,104
5,_deleted,105
7,source,107
9,type,109
11,status,111
20,__v,120
25,subtype,125
29,amount,129
輸出1.csv
id,col_name
1,_id
2,rawrequest
3,rawresponse
4,products
5,_deleted
6,enterpriseid
7,source
8,transactionuid
9,type
10,isotransactiontype
11,status
12,terminalid
13,merchantid
14,merchantname
15,settlementbatchid
16,errordescription
17,referencetransactions
18,createdat
19,updatedat
20,__v
101,_id
102,rawRequest
103,rawResponse
104,products
105,_deleted
106,enterpriseId
107,source
108,transactionUid
109,type
110,isoTransactionType
111,status
112,terminalId
113,merchantId
114,merchantName
115,settlementBatchId
116,errorDescription
117,referenceTransactions
118,createdAt
119,updatedAt
120,__v
輸出2.csv
id,col_name
1,_id
2,rawrequest
3,rawresponse
4,products
5,_deleted
6,enterpriseid
7,source
8,transactionuid
9,type
10,isotransactiontype
11,status
12,terminalid
13,merchantid
14,merchantname
15,settlementbatchid
16,errordescription
17,referencetransactions
18,createdat
19,updatedat
20,__v
id,col_name
101,_id
102,rawRequest
103,rawResponse
104,products
105,_deleted
106,enterpriseId
107,source
108,transactionUid
109,type
110,isoTransactionType
111,status
112,terminalId
113,merchantId
114,merchantName
115,settlementBatchId
116,errorDescription
117,referenceTransactions
118,createdAt
119,updatedAt
120,__v
uj5u.com熱心網友回復:
我在您的代碼中看不到將col_names 轉換為小寫的任何地方,但您肯定需要這樣做才能獲得匹配,因為 egrawrequest不等于rawRequest.
我得到了你想要的輸出,如下所示。
首先,既然您提到它們是帶有列名的 csv,id我col_name假設您的輸入檔案實際上看起來像:
輸入1.csv:
id,col_name
1,_id
2,rawrequest
3,rawresponse
4,products
5,_deleted
...
輸入2.csv:
id,col_name
101,_id
102,rawRequest
103,rawResponse
104,products
105,_deleted
...
保存這些檔案后,我做了:
import pandas as pd
csv1 = pd.read_csv("Input1.csv")
csv2 = pd.read_csv("Input2.csv")
# Method 1
print(csv2.head())
# Make a copy of csv2 col_name
csv2['col_name_original'] = csv2['col_name']
# Convert csv2 col_name to lowercase
csv2['col_name'] = csv2['col_name'].apply(lambda x: x.lower())
# Reorder csv2 columns
csv2 = csv2[['col_name', 'col_name_original', 'id']]
# Merge on col_name
merge_data = csv1.merge(csv2, on='col_name')
# Rename columns of resulting datasheet
merge_data.columns = ['id_', 'col_name', 'col_name', 'id_']
# Save merged data
merge_data.to_csv("output.csv", index=False)
輸出:
id_,col_name,col_name,id_
1,_id,_id,101
2,rawrequest,rawRequest,102
3,rawresponse,rawResponse,103
4,products,products,104
5,_deleted,_deleted,105
您詢問的部分的說明:
Lambda 函式
Lambda 基本上是定義函式的一種簡寫方式,因此在 Python 中定義要轉換為小寫的函式的常用方法是:
def convert_to_lowercase(x):
return x.lower()
相反,我們可以只撰寫lambda x: x.lower()并在那里定義一個函式,然后將其直接傳遞給apply方法。Lambda 運算式也存在于 JavaScript 等其他編程語言中。
https://www.w3schools.com/python/python_lambda.asp
申請
當您使用函式作為引數在 DataFrame 列上呼叫此方法時,它會回傳將該函式應用于列中的每個元素的結果(例如,在這種情況下,它將函式應用于lambda x: x.lower()列中的每個值并col_name用結果逐行)。map如果您熟悉該概念,則相當于 a 。
https://www.datacamp.com/community/tutorials/pandas-apply
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417037.html
標籤:
