Create columns where sum other columns based on conditional of other column values?
NickName:M_F Ask DateTime:2020-05-30T05:15:33

Create columns where sum other columns based on conditional of other column values?

I need to create columns where sum other columns based on conditional of other column values. The requirement is as below.

I have the following table:

key code1  code2 code3 code4 value1 value2 value3 value4 
0    101   101   101   101   1000    1000  1000   1000    
1    101   101   101   201   1000    1000  1000   1000    
2    101   101   201   201   1000    1000  1000   1000    
3    101   201   201   201   1000    1000  1000   1000    
4    101   201   201   301   1000    1000  1000   1000    
5    101   201   301   301   1000    1000  1000   1000    
6    101   301   301   301   1000    1000  1000   1000    
7    101   101   101   301   1000    1000  1000   1000    
8    101   201   301   0     1000    1000  1000   0       
9    101   301   0     0     1000    1000  0      0       
....

I need to create one column to sum the column value(value1, value2, value3, value4) considering the columns code (code1, code2, code3, code4). The result should be like this:

key  code1 code2 code3 code4 value1 value2 value3 value4 sum_code_101 sum_code_201 sum_code_301
0    101   101   101   101   1000    1000  1000   1000     4000           0           0
1    101   101   101   201   1000    1000  1000   1000     3000           1000        0
2    101   101   201   201   1000    1000  1000   1000     2000           2000        0
3    101   201   201   201   1000    1000  1000   1000     1000           3000        0
4    101   201   201   301   1000    1000  1000   1000     1000           2000        1000
5    101   201   301   301   1000    1000  1000   1000     1000           1000        2000
6    101   301   301   301   1000    1000  1000   1000     1000           0           3000
7    101   101   101   301   1000    1000  1000   1000     3000           0           1000
8    101   201   301   0     1000    1000  1000   0        1000           1000        1000
9    101   301   0     0     1000    1000  0      0        1000           0           1000  

As the real table has 25 different codes (101, 201, 301...) I need to create 25 columns to sum their values.

Any help will be very appreciated.

Copyright Notice:Content Author:「M_F」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/62094687/create-columns-where-sum-other-columns-based-on-conditional-of-other-column-valu

More about “Create columns where sum other columns based on conditional of other column values?” related questions

Create columns where sum other columns based on conditional of other column values?

I need to create columns where sum other columns based on conditional of other column values. The requirement is as below. I have the following table: key code1 code2 code3 code4 value1 value2 v...

Show Detail

Pandas: How to sum columns based on conditional of other column values?

I have the following pandas DataFrame. import pandas as pd df = pd.read_csv('filename.csv') print(df) dog A B C 0 dog1 0.787575 0.159330 0.053095 1 dog1...

Show Detail

Create a new column in pandas based on conditional text values from two other columns

How to create a new column in pandas based on conditional text values from two other columns? Initial table - Specialty Category Spec A Cat A Spec A Cat B Spec A Cat C ...

Show Detail

Highlight Cell in one Column BASED on Conditional Formatting of Other Columns

I am a beginner with Macros. I slightly changed my code below to include conditional formatting based on an Input Value. The code below applies a conditional format (orange highlight) to 6 specific

Show Detail

How to create sum of columns in Pandas based on a conditional of multiple columns?

I am trying to sum two columns of the DataFrame to create a third column where the value in the third column is equal to the sum of the positive elements of the other columns. I have tried the belo...

Show Detail

Add column with conditional values located anywhere in certain other columns

I have seen many questions about adding conditional columns to a dataframe, which typically rely on using np.where(). As far as I can tell, np.where() only looks in the same row it's appending to. ...

Show Detail

Pandas: How create columns where sum other columns based on conditional of other column values?

I have the following pandas DataFrame. import pandas as pd df = pd.read_csv('filename.csv') print(df) code1 code2 code3 code4 value1 value2 value3 value4 0 101 101 101 101 1000 1000

Show Detail

Plot Sum of column by values in other columns

I have a dataframe like the sample data below. I would like to create a plot showing the sum of the "Correct" column by each of the other 4 columns, when those columns have value 1. The columns are

Show Detail

Updating column conditional on other columns

My server was crashed, damaging my database. In one of my tables I have an academic_quality column where I store school grades like A, B, C+, D. Somehow, my grades are not distributed through other

Show Detail

Assign column with conditional values based on strings contained in other columns

I am trying to assign a column based on strings that may be contained in other columns. For example var1 = 67 columns = {'col1': ['string1', 'thang2', 'code3', 'string2'],

Show Detail