简体   繁体   English

for 循环,用于根据 Python 中的条件将每一行的值与另一个表中的特定列相乘

[英]for Loop for multiplying a value of each row with a specific column from another table based on condition in Python

I have table A我有A表

NAME姓名 STAGE阶段 Quan Products产品 A一个 B
XYD1234 XYD1234 PROD产品 4643 4643 A, C, T, Y A、C、T、Y 1 1 0 0
FGY4567 FGY4567 TEST测试 4739 4739 A, U, p, Y A, U, p, Y 0 0 1 1
YUD4568 YUD4568 QUE QUE 45632 45632 I, Y, O, P我,Y,O,P 0 0 1 1
OKE4556 OKE4556 STACK 97474 97474 A, C, P, Y A、C、P、Y 1 1 0 0

Table B:表 B:

STAGE阶段 A一个 B
PROD产品 10 10 5 5
TEST测试 20 20 10 10
QUE QUE 30 30 15 15
STACK 49 49 25 25

This is the output I want.这就是我想要的 output。
If the stage of each row matches the stage in table B, then multiply Column A of Table A with column A of table B.如果每行的阶段与表 B 中的阶段匹配,则将表 A 的 A 列与表 B 的 A 列相乘。

NAME姓名 STAGE阶段 Quan Products产品 A一个 B A_STAGE舞台 B_STAGE B_STAGE
XYD1234 XYD1234 PROD产品 4643 4643 A, C, T, Y A、C、T、Y 1 1 0 0 10 10 0 0
FGY4567 FGY4567 TEST测试 4739 4739 A, U, p, Y A, U, p, Y 0 0 1 1 0 0 10 10
YUD4568 YUD4568 QUE QUE 45632 45632 I, Y, O, P我,Y,O,P 0 0 1 1 0 0 15 15
OKE4556 OKE4556 STACK 97474 97474 A, C, P, Y A、C、P、Y 1 1 0 0 49 49 0 0

If this the code I have but it is not working as it should be.如果这是我拥有的代码,但它不能正常工作。

for i in range(0, len(A)):
  A.loc['Stage'] == B['Stage']
  A['A_Stage'] = A['A'] * B['A']
import pandas as pd
df1 = pd.DataFrame({'NAME':['XYD1234','FGY4567','YUD4568','OKE4556'],
                    'STAGE':['PROD','TEST','QUE','STACK'], 
                    'Quan':[4643,4739,45632,97474], 
                    'Products':['A, C, T, Y','A, U, p, Y','I, Y, O, P','A, C, P, Y'], 
                    'A':[1, 0, 0, 1], 
                    'B':[0, 1, 1, 0]})
df2 = pd.DataFrame({'STAGE':['PROD','TEST','QUE','STACK'],
                    'A':[10, 20, 30, 49], 
                    'B':[5, 10, 15, 25]})
df2.columns = ['STAGE', 'A_STAGE', 'B_STAGE']
df1 = df1.merge(df2, on='STAGE')
df1.loc[:,'A_STAGE'] *= df1.loc[:,'A']
df1.loc[:,'B_STAGE'] *= df1.loc[:,'B']

暂无
暂无

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

相关问题 根据每行的计算条件设置列值 - Set Column Value Based on Calculate Condition from Each Row 使用Python熊猫根据条件将行值复制到另一列 - Copy a row value to another column based on condition using Python pandas 基于来自另一个 DataFrame 的列乘以行值 - Multiplying row values based on column from another DataFrame Python-根据条件将一列添加到包含来自另一行的值的数据框 - Python - Add a column to a dataframe containing a value from another row based on condition 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 根据条件转换特定列值,并使用该转换后的值更新另一行 - transform specific column value based on condition and update another row with that transformed value 如何根据条件从 Python 更新 Postgres 表中的列值 - How to update column value in Postgres table from Python based on condition 根据python中的条件使用第1行或第2行的值更新数据框列 - Update dataframe column based with value from either row 1 or row 2 based on condition in python Python Pandas:根据另一列的值更新行 - Python pandas: Updating row based on value from another column 根据 python dataframe 中的特定条件减去特定列的行值 - Subtracting values of a row for a specific column based on a specific condition in python dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM