1

I have two DataFrames, one called df, and another called df_pag. df has the following columns:

Projetos Ano/Volume Unidades

On the other hand, df_pag has the following colums:

Projetos Ano Unidades Paginação

These DataFrames originates from different Data Mining processes. I want to add a new column to df called 'Paginação', where its row value is pulled from df_pag if, and only if, df['Projetos'] = df_pag['Projetos'], df['Ano/Volume'] = df_pag['Ano'] and df['Unidades'] = df_pag['Unidades'].

Here is what I did:

for i in range(len(df.index)):
    for j in range(len(df_pag.index)):
        if df['Projeto'][i] == df_pag['Projeto'][j] and df['Ano/Volume'][i] == df_pag['Ano'][j] and df['Unidade'][i] == df_pag['Unidade'][j]:
            df['Paginação'][i] = df_pag['Páginação'][j]

PS. This is my first question on StackOverflow, therefore, if there is anything unclear please let me know.

1 Answers1

1

Hi what about this approach:

result_df = ((df['Projetos'] == df_pag ['Projetos']) == (df['Ano/Volume'] == df_pag ['Ano'])) == (df['Unidades'] == df_pag ['Unidades'])
df['Paginação'] = df_pag ['Paginação'][result_df]

This places NaN values for locations in Paginação column where the condition in not satisfied.

If you want any other value in place of NaN use .fillna() like:

result_df = ((df['Projetos'] == df_pag ['Projetos']) == (df['Ano/Volume'] == df_pag ['Ano'])) == (df['Unidades'] == df_pag ['Unidades'])
df['Paginação'] = df_pag ['Paginação'][result_df]
df['Paginação'] = df['Paginação'].fillna('my_value')
Abhi_J
  • 2,061
  • 1
  • 4
  • 16