Im trying to merge two csv files (a and b) into one (c). The csv files look like this:
--- CSV A ---
Field_1, Key
a0 , k0
a1 , k1
a2 , k2
a3 , k0
a4 , k2
a5 , k0
--- CSV B ---
Key, Field_2, Field_3
k0 , b0 , c0
k1 , b1 , c1
k2 , b2 , c2
k3 , b3 , c3
--- Expected CSV C (merged csv) ---
Field_1, Key, Field_2, Field_3
a0 , k0 , b0 , c0
a1 , k1 , b1 , c1
a2 , k2 , b2 , c2
a3 , k0 , b0 , c0
a4 , k2 , b2 , c2
a5 , k0 , b0 , c0
So basically the fields from the csv b that matches the key of csv a should be joined to get csv c. But instead Im getting the next merged fields empty
--- Actual CSV C ---
Field_1, Key, Field_2, Field_3
a0 , k0 , ,
a1 , k1 , ,
a2 , k2 , ,
a3 , k0 , ,
a4 , k2 , ,
a5 , k0 , ,
This is the code Im trying to use to merge this fields. But as I said, I am unable to get the data from the csv b merged, I only get the headers.
a = pd.read_csv("a.csv")
b = pd.read_csv("b.csv").rename(columns={'Key': ' Key'})
result = a.merge(b, on=" Key", how="left")
result.to_csv("c.csv", index=False)
So how would I do so I get the data from csv b merged correctly? Thanks.