简体   繁体   English

更新 Dataframe 列的值 Pandas

[英]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.我正在尝试更新我的 pandas dataframe 中的一列,但不知道该怎么做。 I think I need to use .update , but I am not sure how.我想我需要使用.update ,但我不确定如何使用。

My current dataframe looks like this (called auto_df ):我当前的 dataframe 看起来像这样(称为auto_df ):

id ID Make制作 Make (Encoded)制作(编码) Model Model Model (Encoded) Model(编码) Score分数 Price价格
105 105 Kia起亚 14 14 Forte Lx复地Lx 103 103 0.0 0.0 15000 15000
106 106 Nissan日产 21 21 Sentra S森特拉小号 181 181 0.0 0.0 14998 14998
107 107 Toyota丰田 26 26 Avalon Xl阿瓦隆XL 22 22 0.0 0.0 14995 14995
108 108 Chevrolet雪佛兰 4 4个 Camaro Lt科迈罗 Lt 34 34 0.0 0.0 14995 14995
109 109 Kia起亚 14 14 Sportage Lx狮跑Lx 204 204 0.0 0.0 14995 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.我有另一个 dataframe 具有更新的分值(称为auto_df_non ),我想用更新的分值替换此 dataframe 中的 0 值。

auto_df_non looks like this: auto_df_non看起来像这样:

id ID Make制作 Make (Encoded)制作(编码) Model Model Model (Encoded) Model(编码) Score分数 Price价格
105 105 Kia起亚 14 14 Forte Lx复地Lx 103 103 44.439907 44.439907 15000 15000
106 106 Nissan日产 21 21 Sentra S森特拉小号 181 181 27.530042 27.530042 14998 14998
107 107 Toyota丰田 26 26 Avalon Xl阿瓦隆XL 22 22 56.666503 56.666503 14995 14995
108 108 Chevrolet雪佛兰 4 4个 Camaro Lt科迈罗 Lt 34 34 16.844859 16.844859 14995 14995
109 109 Kia起亚 14 14 Sportage Lx狮跑Lx 204 204 14.013014 14.013014 14995 14995

I have tried to get .update to work, but everything I have found on the inte.net so far has not been helpful.我试图让.update工作,但到目前为止我在 inte.net 上找到的所有内容都没有帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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