0

I have two excel files

+ File one contains specific data about different customer (like: Sex, Age, Name...) and 
+ File two contains different transactions for each customer

I want to create a new Column in File2 containing specific data to each Costumer from File1

arajshree
  • 626
  • 4
  • 13
Shahine Greene
  • 196
  • 1
  • 3
  • 15

2 Answers2

1

file1.csv

customer_id,sex,age,name
af4wf3,m,12,mike
z20ask,f,15,sam

file2.csv

transaction_id,customer_id,amount
12h2j4hk,af4wf3,123.20
12h2j4h1,af4wf3,5.22
12h2j4h2,z20ask,13.20
12h2j4h3,af4wf3,1.20
12h2j4h4,z20ask,2341.12
12h2j4h5,z20ask,235.96
12h2j4h6,af4wf3,999.30

Load and join the dataframes

import pandas as pd

df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')

df1.set_index('customer_id', inplace=True)
df2.set_index('transaction_id', inplace=True)

output = df2.join(df1, on='customer_id')
output.to_csv('file2_updated.csv')

file2_updated.csv

transaction_id,customer_id,amount,sex,age,name
12h2j4hk,af4wf3,123.2,m,12,mike
12h2j4h1,af4wf3,5.22,m,12,mike
12h2j4h2,z20ask,13.2,f,15,sam
12h2j4h3,af4wf3,1.2,m,12,mike
12h2j4h4,z20ask,2341.12,f,15,sam
12h2j4h5,z20ask,235.96,f,15,sam
12h2j4h6,af4wf3,999.3,m,12,mike
jc416
  • 117
  • 1
  • 3
0

the same as @jc416 but using pd.merge:

file2.merge(file1, on='customer_id')

    transaction_id  customer_id  amount     sex  age    name
0   12h2j4hk        af4wf3       123.2      m    12     mike
1   12h2j4h1        af4wf3       5.22       m    12     mike
2   12h2j4h3        af4wf3       1.2        m    12     mike
3   12h2j4h6        af4wf3       999.3      m    12     mike
4   12h2j4h2        z20ask       13.2       f    15     sam
5   12h2j4h4        z20ask       2341.12    f    15     sam
6   12h2j4h5        z20ask       235.96     f    15     sam

You definitely should read Pandas merging 101

help-ukraine-now
  • 3,850
  • 4
  • 19
  • 36
  • Thank you so Much it works with merge Now. I don't know why it does not before – Shahine Greene Jul 04 '19 at 17:47
  • @ShahineGreene, glad I could help. Next time you have a question, read this [guide](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) before posting. – help-ukraine-now Jul 04 '19 at 17:49