简体   繁体   中英

Updating Values of Dataframe Column Pandas

I am trying to update a column in my pandas dataframe but cannot figure out how to do it. I think I need to use .update , but I am not sure how.

My current dataframe looks like this (called auto_df ):

id Make Make (Encoded) Model Model (Encoded) Score Price
105 Kia 14 Forte Lx 103 0.0 15000
106 Nissan 21 Sentra S 181 0.0 14998
107 Toyota 26 Avalon Xl 22 0.0 14995
108 Chevrolet 4 Camaro Lt 34 0.0 14995
109 Kia 14 Sportage Lx 204 0.0 14995

I have another dataframe with updated score values (called auto_df_non ), and I want to replace the 0 values in this dataframe with the updated score values.

auto_df_non looks like this:

id Make Make (Encoded) Model Model (Encoded) Score Price
105 Kia 14 Forte Lx 103 44.439907 15000
106 Nissan 21 Sentra S 181 27.530042 14998
107 Toyota 26 Avalon Xl 22 56.666503 14995
108 Chevrolet 4 Camaro Lt 34 16.844859 14995
109 Kia 14 Sportage Lx 204 14.013014 14995

I have tried to get .update to work, but everything I have found on the inte.net so far has not been helpful.

Example

we need minimal and reproducible example for answer. lets make

df1 = pd.DataFrame([['a', 0, 'c'], ['b', 0, 'd']], columns=['col1', 'col2', 'col3'])
df2 = pd.DataFrame([['a', 10, 'e'], ['b', 20, 'f']], columns=['col1', 'col2', 'col4'])

df1

   col1 col2    col3
0   a   0       c
1   b   0       d

df2

   col1 col2    col4
0   a   10      e
1   b   20      f

Code

df1.update(df2)

df1

  col1  col2    col3
0   a   10      c
1   b   20      d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM