Home > database >  How to update a df based in a list. Python
How to update a df based in a list. Python

Time:01-17

I have the next df:


import pandas as pd

data = [{'car' :'audi','id':'ab','year':2001,'wheel':4},
        {'car' :'honda','id':'aa','year':2002,'wheel':15},
        {'car' :'tesla','id':'aaa','year':2003,'wheel':5}]

keys=['a','aa','aaa','avaa','2ffa']

Car ID Year Status Wheels
Audi ab 2001 OK 4
Honda baaa 2002 OK 4
Tesla aaa 2003 OK 4
Tesla avaa 2023 OK 4

So, let's image the next situation: The IDs in the keys list are defective, thus, I have to update the 'Status' column with 'Defective' instead 'Ok' in the appropriate cases.

How can I do that? Thank you!

CodePudding user response:

Use isin combined with boolean indexing:

df.loc[df['id'].isin(keys), 'Status'] = 'Defective'

Updated df:

     car   id  year  wheel     Status
0   audi   ab  2001      4        NaN
1  honda   aa  2002     15  Defective
2  tesla  aaa  2003      5  Defective

Used input:

df = pd.DataFrame(data)

CodePudding user response:

In [220]: data = [{'car' :'audi','id':'ab','year':2001,'wheel':4},
     ...:         {'car' :'honda','id':'aa','year':2002,'wheel':15},
     ...:         {'car' :'tesla','id':'aaa','year':2003,'wheel':5}]
     ...:
     ...: keys=['a','aa','aaa','avaa','2ffa']
     ...:

In [221]: df = pd.DataFrame(data)

In [222]: df
Out[222]:
     car   id  year  wheel
0   audi   ab  2001      4
1  honda   aa  2002     15
2  tesla  aaa  2003      5

In [223]: df['status'] = "OK"

In [224]: df
Out[224]:
     car   id  year  wheel status
0   audi   ab  2001      4     OK
1  honda   aa  2002     15     OK
2  tesla  aaa  2003      5     OK


In [225]: for k in keys:
     ...:     df.loc[df.id==k, 'status'] = "Defective"
     ...:

In [226]: df
Out[226]:
     car   id  year  wheel     status
0   audi   ab  2001      4         OK
1  honda   aa  2002     15  Defective
2  tesla  aaa  2003      5  Defective

CodePudding user response:

You can use np.where with isin:

df['Status'] = np.where(df['id'].isin(keys), 'Defective', 'OK')
print(df)

# Output
     car   id  year  wheel     Status
0   audi   ab  2001      4         OK
1  honda   aa  2002     15  Defective
2  tesla  aaa  2003      5  Defective
  • Related