1

I am trying to create a python code aiming to match 2 tables puit_1 and puit_2 in order to retrieve the identifier id of the table puit_2 by creating a new column id in the table puit_1, if the name of the table puit_1 equals the table puit_2 . below the loop :

puits_sig[id_credo] = pd.series([])
for j in range(len(puits_credo['noms'])):
    id_pcredo = []
    for i in range(len(puits_sig['sigle_puits'])):
        if puits_credo['noms'][j] == puits_credo['noms'][j]:
            if str(puits_sig['sigle_puits'][i]) in puits_credo['noms'][j]:
               id_pcredo.append(puits_credo['ID_Objet'][j]) 
               print(id_pcredo)
puits_sig['id_credo'] = id_pcredo

ValueError : length of values 1 does not match length of index 1212

This code gives me an error that I couldn't solve (for information, I'm a beginner in programming).

Any help below some extract from tables? Extract from table 1

[sigle_puit] [categorie]
BNY2D ACTIF
BRM2 INACTIF

Extract from table 2

[Nom] [ID Object]
BLY23 89231
BRM1 12175
IAmri
  • 11
  • 4

1 Answers1

0

What I believe you're looking for is an inner-merge Pandas operation. What this does is create a new dataframe which has the 'name' column which both puit_1 and puit_2 have in common, and all other information which comes from the columns in dataframes 1 and 2.

merged_df = puit_1.merge(puit_2, how = 'inner', on = ['name'])

See this question for more: Merge DataFrames with Matching Values From Two Different Columns - Pandas

EDIT: If you want to keep rows which have a name which only exists in one dataframe, you might want to use how='outer' in your merge operation