简体   繁体   English

根据条件转换特定列值,并使用该转换后的值更新另一行

[英]transform specific column value based on condition and update another row with that transformed value

I have a dataframe that looks something like: 我有一个类似于以下内容的数据框:

Obj_1   Obj_2   Obj_3   Num_1    Num_2     Num_3   Month

A       BCD      QW      111       222     3456    2018-12
B       Bdfh     AQW     1114     1222     23456    2018-12
A       BCD      QW      22222     67      3463    2019-01
B       Bdfh     AQW     15511    2422     13456    2019-01
A       BCD      QW      257      457867    34663   2019-02
B       Bdfh     AQW     11551     27722    53456    2019-02
.....
.....
....

I would like to apply some basic math computation such that if Obj_3 == 'AQW' and Month == '2018-12', then pick the corresponding Num_3 value and multiply by 2, and assign that to the row that corresponds to 2019-02 with the same Obj columns combinations. 我想应用一些基本的数学计算,如果Obj_3 =='AQW'和月=='2018-12',则选择相应的Num_3值并乘以2,并将其分配给对应于2019-的行 - 02具有相同的Obj列组合。

So the output would look something like : 所以输出看起来像:

Obj_1   Obj_2   Obj_3   Num_1    Num_2     Num_3_adj   Month

A       BCD      QW      111       222     3456    2018-12
B       Bdfh     AQW     1114     1222     23456    2018-12
A       BCD      QW      22222     67      3463    2019-01
B       Bdfh     AQW     15511    2422     13456    2019-01
A       BCD      QW      257      457867    34663   2019-02
B       Bdfh     AQW     11551     27722    46912    2019-02
.....
.....
....

I am thinking of first ordering the dataframe by object columns, then assign a flag or counter to unique object row combinations. 我想首先按对象列排序数据帧,然后为唯一对象行组合分配一个标志或计数器。 Post that filter for the condition and assign it to a row where the counter matches and the Month matches the desired type. 发布该条件的过滤器并将其分配给计数器匹配的行,并且Month与所需类型匹配。

You can achieve the expected output with the following code, 您可以使用以下代码实现预期输出,

>>> df.loc[df.query('Obj_3 == "AQW" and Month == "2019-02"').index,'Num_3']=df.query('Obj_3 == "AQW" and Month == "2018-12"')['Num_3'].mul(2).values

>>> df

  Obj_1 Obj_2 Obj_3  Num_1   Num_2  Num_3    Month
0     A   BCD    QW    111     222   3456  2018-12
1     B  Bdfh   AQW   1114    1222  23456  2018-12
2     A   BCD    QW  22222      67   3463  2019-01
3     B  Bdfh   AQW  15511    2422  13456  2019-01
4     A   BCD    QW    257  457867  34663  2019-02
5     B  Bdfh   AQW  11551   27722  46912  2019-02
>>> 

暂无
暂无

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

相关问题 根据条件使用另一个数据帧列值更新一个数据帧值 - Update one dataframe value with another dataframe column value based on the condition 获取具有基于另一列的条件的行的列值 - Get column value of row with condition based on another column for 循环,用于根据 Python 中的条件将每一行的值与另一个表中的特定列相乘 - for Loop for multiplying a value of each row with a specific column from another table based on condition in Python 使用Python熊猫根据条件将行值复制到另一列 - Copy a row value to another column based on condition using Python pandas 根据列值熊猫的条件更新特定列的单元格值 - Update cell values of specific columns based on condition of column value pandas 在Pandas中,如何根据另一行中的另一列值更新一行中的列值 - In Pandas how to update column value in one row based on another column value in another row 根据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 如何根据行中的特定值和熊猫中的另一列对行进行分组? - How to group rows based on specific value in a row and another column in pandas? 根据python中的条件使用第1行或第2行的值更新数据框列 - Update dataframe column based with value from either row 1 or row 2 based on condition in python 如果满足条件,则用列名更新行值 - Update row value with column name if condition is met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM