Home > OS >  Replace from database with python
Replace from database with python

Time:12-29

I'm stuck with this issue:

I want to replace each row in one column in csv with id.

I have vehicle names and id's in the database:

enter image description here

In csv file this column look like this:

enter image description here

I was thinking to use pandas, to make a replacement:

df = pd.read_csv(file).replace('ALFA ROMEO 147 (937), 10.04 - 05.10', '0')

But it is the wrong way to write replace 2000 times.

So, how can I use names from db and replace them with the correct id?

CodePudding user response:

A possible solution is to merge the second dataset with the first one: After reading the two datasets (df1, the one from the csv file, and df2, the one with vehicle_id):

df1.merge(df2, how='left', on='vehicle')

So that the final output will be a dataset with columns: id, vehicle, vehicle_id

Imagine df1 as:

df1

and df2 as:

df2

the result will be: enter image description here

Here you can find the documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html

  • Related