Python Pandas CSVs

Question:

I have some csv files. they have one common column item_id.

How can I merge the csvs to get columns item_id, weight, location, price?

With concat I have multiple same id and columns with NaN…

Here I try to create some csv and merge it to one

item_id = [1342,35513,5135,5351]
weight = [10,20,30,40]
location = ['A','B','B','A']
fcsv = {
    'id': item_id,
             'loc': location,
             'weight': weight}
fcsv
fcsv_df = pd.DataFrame(fcsv)
fcsv_df
item_id =[35513,1342,5135,5351]
weight = [20,10,30,40]
price = [10555,25550,35550,4550]

fcsv2 = {'id': item_id,
    'weight': weight,
             'price': price,
            }
fcsv2
fcsv2_df = pd.DataFrame(fcsv2)

fcsv_df.to_csv('csv1.csv')
fcsv2_df.to_csv('csv2.csv')



file = pd.concat([fcsv_df,fcsv2_df])
file

Here i try to get something like this…

1342,10,A,25550

35513,20,B,10555,

5135,30,B,35550,

5351,40,A,4550

Please tell me witch method i can use to do it. I try to find it second hour and 0…

Asked By: ADUNAC

||

Answers:

you should use merge for ID matching:

file = pd.merge(fcsv_df, fcsv2_df, how='left', on="id")

out:

      id loc  weight_x  weight_y  price
0   1342   A        10        10  25550
1  35513   B        20        20  10555
2   5135   B        30        30  35550
3   5351   A        40        40   4550
Answered By: NoobVB
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.