0

Well, that's my first use of Stack. Sorry if i double an already topic but if i found into stack how to compare two columns i don't know how to finish my work. I have to csv, A and B. In A there is a list of name and ids, and in B only a list of names with other informations. The two files doesn't have the same number of lines. I want to write a python script that read A and B files, and write into a dedicated column, the ids it founds in A if A name and B name are the same. I don't want to use excel, i m tryin to improve my python skills as i m using more and more matplotlib, pandas, seaborn for dataviz. Any idea? Thanks a lot and sorry if i misused the stackoverflow questions system

Trix

Tristan Salord
  • 57
  • 1
  • 10
  • 2
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael May 10 '18 at 13:54
  • Hello i think i found a partial solution: what do u think of that?: – Tristan Salord May 10 '18 at 14:35
  • import pandas as pd – Tristan Salord May 10 '18 at 14:35
  • sorry for the multicomment i m new here: so the solution i found is using merge, i can put my script if i can find how.... – Tristan Salord May 10 '18 at 14:36

1 Answers1

0

Use merge:

import pandas as pd

a = pd.read_csv(r'path to a.csv')
b = pd.read_csv(r'path to b.csv')

df = pd.merge(a,b, on='Name', how='inner')

use the different types of joins in how to get desired result.

It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
  • thank you very much Chris, i found the same solution except for the function how'inner': what it means? – Tristan Salord May 10 '18 at 17:58
  • @TristanSalord the how is strictly how you want to join your data. `Inner` joins will only keep records that are found in both data sets. `Outer` joins will match up the records that match and keep the ones that do no. There are also left and right joins. It comes from SQL. Read the panadas.merge documentation to learn more. https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html – It_is_Chris May 10 '18 at 19:24
  • Ah oki! Thanks! Yes i found it was like when i work with oracle sql. Thank you for ur response! it helped a lot and i learned new things! – Tristan Salord May 10 '18 at 20:34