Skip to main content
adicionou 1326 caracteres ao conteúdo
Fonte Link
lmonferrari
  • 4mil
  • 1
  • 9
  • 19

df

    CNPJ        DATA              codprojeto
0   123  2020-12-02 00:00:00 UTC      0
1   123  2020-12-02 00:00:00 UTC      0
2   123  2020-12-02 00:00:00 UTC      0
3   123  2020-12-02 00:00:00 UTC      0
4   145  2020-12-02 00:00:00 UTC      0
5   123  2020-12-02 00:00:00 UTC      0
6   167  2020-12-02 00:00:00 UTC      0
7   167  2020-12-02 00:00:00 UTC      0
8   167  2020-12-02 00:00:00 UTC      0
9   167  2020-12-02 00:00:00 UTC      0
10  101  2020-12-02 00:00:00 UTC      0
11  122  2020-12-02 00:00:00 UTC      0
12  144  2020-12-02 00:00:00 UTC      0
13  123  2020-12-02 00:00:00 UTC      0
14  155  2020-12-02 00:00:00 UTC      0
15  155  2020-12-02 00:00:00 UTC      0
16  155  2020-12-02 00:00:00 UTC      0
17  166  2020-12-02 00:00:00 UTC      0
18  123  2020-12-02 00:00:00 UTC      0
19  123  2020-12-02 00:00:00 UTC      0

df2

    codcliente  nome        CNPJ    codprojeto
0   1           CLIENTE 1   123     1234
1   2           CLIENTE 1   145     5678
2   3           CLIENTE 1   167     9012
3   4           CLIENTE 1   189     3456
4   5           CLIENTE 1   101     7890
5   6           CLIENTE 1   122       11
6   7           CLIENTE 1   133       22
7   8           CLIENTE 1   144       33
8   9           CLIENTE 9   155       44
9   10          CLIENTE 10  166       55

Mapeando a Serie de df para atribuir os valores à correspondência no df2

Mapeando a Serie de df para atribuir os valores à correspondência no df2

df

    CNPJ        DATA              codprojeto
0   123  2020-12-02 00:00:00 UTC      0
1   123  2020-12-02 00:00:00 UTC      0
2   123  2020-12-02 00:00:00 UTC      0
3   123  2020-12-02 00:00:00 UTC      0
4   145  2020-12-02 00:00:00 UTC      0
5   123  2020-12-02 00:00:00 UTC      0
6   167  2020-12-02 00:00:00 UTC      0
7   167  2020-12-02 00:00:00 UTC      0
8   167  2020-12-02 00:00:00 UTC      0
9   167  2020-12-02 00:00:00 UTC      0
10  101  2020-12-02 00:00:00 UTC      0
11  122  2020-12-02 00:00:00 UTC      0
12  144  2020-12-02 00:00:00 UTC      0
13  123  2020-12-02 00:00:00 UTC      0
14  155  2020-12-02 00:00:00 UTC      0
15  155  2020-12-02 00:00:00 UTC      0
16  155  2020-12-02 00:00:00 UTC      0
17  166  2020-12-02 00:00:00 UTC      0
18  123  2020-12-02 00:00:00 UTC      0
19  123  2020-12-02 00:00:00 UTC      0

df2

    codcliente  nome        CNPJ    codprojeto
0   1           CLIENTE 1   123     1234
1   2           CLIENTE 1   145     5678
2   3           CLIENTE 1   167     9012
3   4           CLIENTE 1   189     3456
4   5           CLIENTE 1   101     7890
5   6           CLIENTE 1   122       11
6   7           CLIENTE 1   133       22
7   8           CLIENTE 1   144       33
8   9           CLIENTE 9   155       44
9   10          CLIENTE 10  166       55

Mapeando a Serie de df para atribuir os valores à correspondência no df2

removeu 2 caracteres do conteúdo
Fonte Link
lmonferrari
  • 4mil
  • 1
  • 9
  • 19

Importando o pandas

import pandas as pd

Carregando os arquivos de teste

df = pd.read_csv('./df.csv', sep = ';')
df2 = pd.read_csv('./df2.csv', sep = ';')

Mapeando a Serie de df para atribuir os valores à correspondência no df2

df2['codprojeto']df['codprojeto'] = df2['CNPJ']df['CNPJ'].map(dfdf2.set_index('CNPJ')['codprojeto']).fillna(0)

Imprimindo

df2df

Saída:

    CNPJ             DATA           codprojeto
0   123   2020-12-02 00:00:00 UTC   1234
1   123   2020-12-02 00:00:00 UTC   1234
2   123   2020-12-02 00:00:00 UTC   1234
3   123   2020-12-02 00:00:00 UTC   1234
4   145   2020-12-02 00:00:00 UTC   5678
5   123   2020-12-02 00:00:00 UTC   1234
6   167   2020-12-02 00:00:00 UTC   9012
7   167   2020-12-02 00:00:00 UTC   9012
8   167   2020-12-02 00:00:00 UTC   9012
9   167   2020-12-02 00:00:00 UTC   9012
10  101   2020-12-02 00:00:00 UTC   7890
11  122   2020-12-02 00:00:00 UTC     11
...

Importando o pandas

import pandas as pd

Carregando os arquivos de teste

df = pd.read_csv('./df.csv', sep = ';')
df2 = pd.read_csv('./df2.csv')

Mapeando a Serie de df para atribuir os valores à correspondência no df2

df2['codprojeto'] = df2['CNPJ'].map(df.set_index('CNPJ')['codprojeto']).fillna(0)

Imprimindo

df2

Saída:

    CNPJ             DATA           codprojeto
0   123   2020-12-02 00:00:00 UTC   1234
1   123   2020-12-02 00:00:00 UTC   1234
2   123   2020-12-02 00:00:00 UTC   1234
3   123   2020-12-02 00:00:00 UTC   1234
4   145   2020-12-02 00:00:00 UTC   5678
5   123   2020-12-02 00:00:00 UTC   1234
6   167   2020-12-02 00:00:00 UTC   9012
7   167   2020-12-02 00:00:00 UTC   9012
8   167   2020-12-02 00:00:00 UTC   9012
9   167   2020-12-02 00:00:00 UTC   9012
10  101   2020-12-02 00:00:00 UTC   7890
11  122   2020-12-02 00:00:00 UTC     11
...

Importando o pandas

import pandas as pd

Carregando os arquivos de teste

df = pd.read_csv('./df.csv')
df2 = pd.read_csv('./df2.csv', sep = ';')

Mapeando a Serie de df para atribuir os valores à correspondência no df2

df['codprojeto'] = df['CNPJ'].map(df2.set_index('CNPJ')['codprojeto']).fillna(0)

Imprimindo

df

Saída:

    CNPJ             DATA           codprojeto
0   123   2020-12-02 00:00:00 UTC   1234
1   123   2020-12-02 00:00:00 UTC   1234
2   123   2020-12-02 00:00:00 UTC   1234
3   123   2020-12-02 00:00:00 UTC   1234
4   145   2020-12-02 00:00:00 UTC   5678
5   123   2020-12-02 00:00:00 UTC   1234
6   167   2020-12-02 00:00:00 UTC   9012
7   167   2020-12-02 00:00:00 UTC   9012
8   167   2020-12-02 00:00:00 UTC   9012
9   167   2020-12-02 00:00:00 UTC   9012
10  101   2020-12-02 00:00:00 UTC   7890
11  122   2020-12-02 00:00:00 UTC     11
...
Fonte Link
lmonferrari
  • 4mil
  • 1
  • 9
  • 19

Importando o pandas

import pandas as pd

Carregando os arquivos de teste

df = pd.read_csv('./df.csv', sep = ';')
df2 = pd.read_csv('./df2.csv')

Mapeando a Serie de df para atribuir os valores à correspondência no df2

df2['codprojeto'] = df2['CNPJ'].map(df.set_index('CNPJ')['codprojeto']).fillna(0)

Imprimindo

df2

Saída:

    CNPJ             DATA           codprojeto
0   123   2020-12-02 00:00:00 UTC   1234
1   123   2020-12-02 00:00:00 UTC   1234
2   123   2020-12-02 00:00:00 UTC   1234
3   123   2020-12-02 00:00:00 UTC   1234
4   145   2020-12-02 00:00:00 UTC   5678
5   123   2020-12-02 00:00:00 UTC   1234
6   167   2020-12-02 00:00:00 UTC   9012
7   167   2020-12-02 00:00:00 UTC   9012
8   167   2020-12-02 00:00:00 UTC   9012
9   167   2020-12-02 00:00:00 UTC   9012
10  101   2020-12-02 00:00:00 UTC   7890
11  122   2020-12-02 00:00:00 UTC     11
...