简体   繁体   English

如何根据条件从另一个 Dataframe 更新 Dataframe 值

[英]How to update a Dataframe values from an another Dataframe based on condition

I'm trying to update a "qty" column in a Dataframe based on another Dataframe "qty" column only for specific rows (according to specific types).我正在尝试更新 Dataframe 中基于另一个 Dataframe “数量”列的“数量”列,仅为特定行(根据特定类型)。

Here are my example Dataframes:这是我的示例数据框:

df = pd.DataFrame({'op': ['A', 'A', 'A', 'B', 'C'], 'type': ['X', 'Y', 'Z', 'X', 'Z'], 'qty': [3, 1, 8, 0, 4]})
df_xy = pd.DataFrame({'op': ['A', 'B', 'C'], 'qty': [10, 20, 30]})
print(df)
print(df_xy)

  op type  qty
0  A    X    3
1  A    Y    1
2  A    Z    8
3  B    X    0
4  C    Z    4

  op  qty
0  A   10
1  B   20
2  C   30

I try to use the loc function to choose the concerned rows and to compare with the other Dataframe according to my reference column "op" but without success我尝试使用 loc function 选择相关行并根据我的参考列“op”与其他 Dataframe 进行比较但没有成功

# Select df rows where "type" is in "types" and set "qty" according to "qty" from df_xy
types = ['X', 'Y']
df.loc[df['type'].isin(types), 'qty'] = df_xy.loc[df_xy['op'] == df['op'], 'qty']
print(df)

I would like to have a Dataframe that is like this:我想要一个 Dataframe 是这样的:

  op type  qty
0  A    X    10
1  A    Y    10
2  A    Z    8
3  B    X    20
4  C    Z    4

But I have an error specifying that I cannot compare Series Objects that are not labeled the same way但是我有一个错误,指出我无法比较未以相同方式标记的系列对象

ValueError: Can only compare identically-labeled Series objects

Any help is much appreciated!任何帮助深表感谢! Thanks in advance!提前致谢!

Use Series.map only for filtered rows in both sides for avoid processing not matched rows, here Z rows:仅对两侧的过滤行使用Series.map以避免处理不匹配的行,这里是Z行:

types = ['X', 'Y']
mask = df['type'].isin(types)
df.loc[mask, 'qty'] = df.loc[mask, 'op'].map(df_xy.set_index('op')['qty'])
print (df)
  op type  qty
0  A    X   10
1  A    Y   10
2  A    Z    8
3  B    X   20
4  C    Z    4

You could combine loc and merge to align your 2 Series:您可以结合locmerge来对齐您的 2 系列:

df.loc[df['type'].isin(types), 'qty'] = df[['op']].merge(df_xy, on='op')['qty']

output: output:

  op type  qty
0  A    X   10
1  A    Y   10
2  A    Z    8
3  B    X   20
4  C    Z    4

暂无
暂无

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

相关问题 根据来自另一个数据帧的值更新数据帧 - Update a dataframe based on values from another dataframe 如何根据使用 Pyspark 的条件从另一个表更新 Spark DataFrame 表的列值 - How to update Spark DataFrame Column Values of a table from another table based on a condition using Pyspark 根据条件从另一个数据帧的值替换一个数据帧的值 - substitue values of one dataframe from values of another dataframe based on condition 根据条件从另一个数据帧的值向数据帧添加新列 - Adding a new column to a dataframe from the values of another dataframe based on a condition 根据另一个 dataframe 和条件将值输入 dataframe - Imputing values into a dataframe based on another dataframe and a condition 如何基于另一个DataFrame中的值更新DataFrame中的值? - how to update values in a DataFrame based on values in another DataFrame? 根据另一个 dataframe 的条件更新 dataframe 的值 - Update values of a dataframe based on conditions from another dataframe 如何根据来自另一个 dataframe 的条件从 dataframe 检索单元格 - How to retrieve cells from a dataframe based on condition from another dataframe 如何根据 Python 中的条件将来自另一个 dataframe 的值分配给当前 dataframe? - How to assign values from another dataframe to current dataframe based on a condition in Python? 如何基于GroupBy条件从另一个数据框创建一个数据框 - How to create a dataframe from another dataframe based on GroupBy condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM