pythonpython-3.xpandas

How to create a new column based on values from other columns in a Pandas DataFrame


I'm new to programming and Pandas. Therefore, please do not judge strictly.

To this table, I need to add a new column of values got from other columns.

inp = [{'Date':2003, 'b1':5,'b2':0,'b3':4,'b4':3},{'Date':2003, 'b1':2,'b2':2,'b3':1,'b4':8},{'Date':2004, 'b1':2,'b2':3,'b3':1,'b4':1},{'Date':2004, 'b1':1,'b2':8,'b3':2,'b4':1},{'Date':2005, 'b1':2,'b2':1,'b3':6,'b4':2},{'Date':2006, 'b1':1,'b2':7,'b3':2,'b4':9}]
df = pd.DataFrame(inp)
print (df)

   Date  b1  b2  b3  b4
0  2003   5   0   4   3
1  2003   2   2   1   8
2  2004   2   3   1   1
3  2004   1   8   2   1
4  2005   2   1   6   2
5  2006   1   7   2   9

Namely, depending on the date. That is if the value of column "Date" == 2003 - I need to get the value from column b1, if the value of column "Date" = 2004 then I need to get the value from column b2, "Date" = 2004 - column b3 and so on. So the values of new column should be: 5,2,3,8,6,9.

I have a dictionary of correspondences smt. like:

Corr_dict = {2003:'b1',2004:'b2',2005:'b4',2006:'b7'...}

This is just an example. I have a large dataset, so I want to understand the mechanics.

Sorry for the poor question formatting. I will be very grateful for any help.

expected output

   Date  b1  b2  b3  b4  vals
0  2003   5   0   4   3   5.0
1  2003   2   2   1   8   2.0
2  2004   2   3   1   1   3.0
3  2004   1   8   2   1   8.0
4  2005   2   1   6   2   6.0
5  2006   1   7   2   9   9.0

Solution

  • I'd use df.lookup:

    df['Correspond'] = df.lookup(df.index, df['Date'].map(dd))
    

    MCVE:

    import pandas as pd
    
    import numpy as np
    
    inp = [{'Date':2003, 'b1':5,'b2':0,'b3':4,'b4':3},{'Date':2003, 'b1':2,'b2':2,'b3':1,'b4':8},{'Date':2004, 'b1':2,'b2':3,'b3':1,'b4':1},{'Date':2004, 'b1':1,'b2':8,'b3':2,'b4':1},{'Date':2005, 'b1':2,'b2':1,'b3':6,'b4':2},{'Date':2006, 'b1':1,'b2':7,'b3':2,'b4':9}]
    df = pd.DataFrame(inp)
    
    dd = {2003:'b1', 2004:'b2', 2005:'b3', 2006:'b4'}
    
    df['Correspond'] = df.lookup(df.index, df['Date'].map(dd))
    print(df)
    

    output:

       Date  b1  b2  b3  b4  Correspond
    0  2003   5   0   4   3           5
    1  2003   2   2   1   8           2
    2  2004   2   3   1   1           3
    3  2004   1   8   2   1           8
    4  2005   2   1   6   2           6
    5  2006   1   7   2   9           9