0

I have a dataframe with headers 'Category', 'Factor1', 'Factor2', 'Factor3', 'Factor4', 'UseFactorA', 'UseFactorB'.

The value of 'UseFactorA' and 'UseFactorB' are one of the strings ['Factor1', 'Factor2', 'Factor3', 'Factor4'], keyed based on the value in 'Category'.

I want to generate a column, 'Result', which equals dataframe[UseFactorA]/dataframe[UseFactorB]

Take the below dataframe as an example:

[Category] [Factor1] [Factor2] [Factor3] [Factor4] [useFactor1] [useFactor2]
     A         1        2         5           8     'Factor1'    'Factor3'
     B         2        7         4           2     'Factor3'    'Factor1'

The 'Result' series should be [2, .2]

However, I cannot figure out how to feed the value of useFactor1 and useFactor2 into an index to make this happen--if the columns to use were fixed, I would just give

df['Result'] = df['Factor1']/df['Factor2']

However, when I try to give

df['Results'] = df[df['useFactorA']]/df[df['useFactorB']]

I get the error

ValueError: Wrong number of items passed 3842, placement implies 1

Is there a method for doing what I am trying here?

2 Answers 2

1

Probably not the prettiest solution (because of the iterrows), but what comes to mind is to iterate through the sets of factors and set the 'Result' value at each index:

for i, factors in df[['UseFactorA', 'UseFactorB']].iterrows():
    df.loc[i, 'Result'] = df[factors['UseFactorA']] / df[factors['UseFactorB']]

Edit:

Another option:

def factor_calc_for_row(row):
    factorA = row['UseFactorA']
    factorB = row['UseFactorB']
    return row[factorA] / row[factorB]

df['Result'] = df.apply(factor_calc_for_row, axis=1)
Sign up to request clarification or add additional context in comments.

1 Comment

Function worked perfectly, and very simply! I tried something similar, but left out the axis=1 argument--thank you very much.
1

Here's the one liner:

df['Results'] = [df[df['UseFactorA'][x]][x]/df[df['UseFactorB'][x]][x] for x in range(len(df))]

How it works is:

df[df['UseFactorA']]

Returns a data frame,

df[df['UseFactorA'][x]]

Returns a Series

df[df['UseFactorA'][x]][x]

Pulls a single value from the series.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.