I'm relativly new to python and pandas, so I might not have the full understanding of all possibilities and would appreciate a hint how to solve the following problem:
I have a df
like this one:
Jan Feb Mar Apr i j
a 100 200 250 100 1 0.3
b 120 130 90 100 3 0.7
c 10 30 10 20 2 0.25
I want to construct a column which takes the column with the index according to df['i']
and then multiplies the value in the selected column with the value in df['j']
.
I want to bulid a table like this (df['k']
beeing the column constructed):
Jan Feb Mar Apr i j k
a 100 200 250 100 1 0.3 60
b 120 130 90 100 3 0.7 70
c 10 30 10 20 2 0.25 2.5
(row a
df['k']=200*0.3
(df['Feb']*df['j']
), in row b
df['k']=100*0.7
(df['Apr']*df['j']
) and in row c
df['k']=10*0.25
(df['Mar']*df['j']
))
The value in df['i']
will always be an integer value, so i would love to use the position of a column according to the value in df['i']
.
IIUC, DataFrame.rename
and then we can use DataFrame.lookup
to map
. Finally we use Series.mul
df['k'] = df['j'].mul(df.rename(columns = dict(zip(df.columns,
range(len(df.columns)))))
.lookup(df.index, df['i']))
print(df)
Output
Jan Feb Mar Apr i j k
a 100 200 250 100 1 0.30 60.0
b 120 130 90 100 3 0.70 70.0
c 10 30 10 20 2 0.25 2.5
Alternative:
df['j'].mul(df.iloc[:,df['i']].lookup(df.index,
df['i'].map(dict(zip(range(len(df.columns)),
df.columns)))))