I have a dataframe that is similar to the example below:
sample = {'col1': [50.6, 30.67, 40.5, 0, 0, 0],
'col2': [40.74, 30.33, 41.00, 0, 0, 0]}
df_sample = pd.DataFrame(sample)
Now, in both col2 and col3 however, the entries represent two different values. For example, for entry 50.6, that represents val1 = 5 and val2 = 0.6. Another example would be 41.00. This value represents 4 and 1.0.
Basically, what I want to get is a column which can be computed as follows:
df_sample['res'] = df_sample.apply(lambda x:
((x['col2']//10)*(x['col2']%10) + (x['col3']//10)*(x['col3']%10))
/ (x['col2']//10 + x['col3']//10), axis=1)
df_sample.fillna(0)
Basically, it gets the weighted average from the values obtained from each column. Now, what I want to do is scale this method to work with let's say twenty columns without hardcoding each column name in the DataFrame. Please advise.