Pandas: Dataframe information comparison

Asked

Viewed 806 times

2

I have 2 Dataframes imported from CSV

CSV1

4616; CCIVIL_03/decreto/2003/D4616.htm
4617; CCIVIL_03/decreto/2003/D4617.htm
4618; CCIVIL_03/decreto/2003/D4618.htm
4619; CCIVIL_03/decreto/2003/D4619.htm
4620; CCIVIL_03/decreto/2003/D4620.htm
4620impressao; CCIVIL_03/decreto/2003/D4620impressao.htm
4621; CCIVIL_03/decreto/2003/D4621.htm
4622; CCIVIL_03/decreto/2003/D4622.htm
4623; CCIVIL_03/decreto/2003/D4623.htm
4624; CCIVIL_03/decreto/2003/D4624.htm
4625; CCIVIL_03/decreto/2003/D4625.htm

CSV2

04456; 2002; CCIVIL_03/Atos/decretos/2002/D04456.html
05931; 2006; CCIVIL_03/Atos/decretos/2006/D05931.html
05940; 2006; CCIVIL_03/Atos/decretos/2006/D05940.html
05922; 2006; CCIVIL_03/Atos/decretos/2006/D05922.html
05933; 2006; CCIVIL_03/Atos/decretos/2006/D05933.html
05934; 2006; CCIVIL_03/Atos/decretos/2006/D05934.html
05937; 2006; CCIVIL_03/Atos/decretos/2006/D05937.html
05921; 2006; CCIVIL_03/Atos/decretos/2006/D05921.html
05932; 2006; CCIVIL_03/Atos/decretos/2006/D05932.html

Follow the implemented code:

import pandas as pd
df_acervo1 = pd.read_csv('../atos_publicados_dec_url_num.csv', sep=';', names=['num', 'url'])
df_acervo3 = pd.read_csv('../atos_publicados_dec_sican_url_num_ano.csv', sep=';',
                         names=['num', 'ano', 'url'])

How can I associate the two CSV, where there is matching numbering? in this way:

num, ano, url_CSV1, url_CSV2
2204, 1985, CCIVIL_03/decreto/1851-1899/D02204.html,  CCIVIL_03/Atos/decretos/1895/D02204.html
  • What would be "numbering correspondence"? His final example has the year 1985, but he does not appear in either of the two Csvs; where did he come from?

  • @Andersoncarloswoss. Matching numbering would be num_CSV1 = num_CSV2, and year belongs to CSV2; the final example is an expectation result.

1 answer

3


Brito, in this case Voce can use the Pandas function - Merge

the code must be:

df_join = pd.merge(df_acervo1, df_acervo3, how ='inner', on = ['num'])

obs. The parameter of ON above represents the key between the two columns

Obs2. In the parameter HOW Voce has to put the type of Join you want in the same way as in the database (RIGHT, LEFT, INNER)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.