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