3

I found (How do I merge two data frames in Python Pandas?), but do not get the expected result.

I have these two CSV files:

# f1.csv
num   ano
76971  1975
76969  1975
76968  1975
76966  1975
76964  1975
76963  1975
76960  1975

and

# f2.csv
num   ano   dou  url
76971  1975 p1   http://exemplo.com/page1
76968  1975 p2   http://exemplo.com/page10
76966  1975 p2   http://exemplo.com/page100

How do I merge these for to get the result given below?

# Expected result
num   ano   dou  url
76971  1975 p1   http://exemplo.com/page1
76969  1975
76968  1975 p2   http://exemplo.com/page10
76966  1975 p2   http://exemplo.com/page100
76964  1975
76963  1975
76960  1975
Stephen Rauch
  • 1,783
  • 11
  • 22
  • 34
britodfbr
  • 163
  • 1
  • 4

1 Answers1

7

f1.merge(f2, left_on='num', right_on='num', how='outer')

see https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html

william3031
  • 211
  • 1
  • 4
  • 2
    very thanks! I finish below: a1975.merge(b1975, left_on=['num', 'ano'], right_on=['num', 'ano'], how='outer', sort=True) – britodfbr Nov 17 '18 at 13:46
  • 1
    Great tutorial for this kind of question.

    https://www.shanelynn.ie/merge-join-dataframes-python-pandas-index-1/

    – ASH Feb 22 '20 at 14:08