How to create a new column based on values from other columns in a Pandas DataFrame
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
IIUC, I would write a function for that:
def extract(df, year):
min_year = df['Date'].min()
return df.loc[df['Date']==year, df.columns[year+1 - min_year]]
extract(df, 2003)
# 0 5
# 1 2
# Name: b1, dtype: int64
And for all year as a colunms:
pd.concat(extract(df, year).rename('new_col') for year in df['Date'].unique())
Output:
0 5
1 2
2 3
3 8
4 6
5 9
Name: new_col, dtype: int64
IIUC
s=df.set_index('Date').stack()
df['New']=s[s.index.isin(list(d.items()))].values