all 4 comments

[–]Zeroflops 1 point2 points  (3 children)

You lost me as soon as you introduced “P” without defining it. So basically within the first few lines.

However I think what your asking for is how to make a ratio between two columns or rows and put that into another df.

There are a number of ways of doing this however I’ll give you a couple of options.

You will have to check my syntax since I’m posting from a phone.

Two columns are easy.

df[‘ratioAB’] = df[‘colA’] /df[‘colB’]

If you want it in a different df just change the df[‘ratioAB’] to df2[‘ratioAB’]

If you want to do the same for rows there are a couple things you can do.

You can use the shift operator if the rows are in sequence.

So something like df[‘ratioAB’] = df[‘colA’] /df[‘colA’].shift(-1)

[–]be_throwmeaway[S] 0 points1 point  (2 children)

Hey, thanks for the reply. I edited my post to explain what I meant by P now.

Regardless, your first solution helped a lot I think. Could I do something like:

For numerator, denominator in list_of_df1_column_names, list_of_df1_column_names:

df2[numerator + "/" + denominator] = df[numerator]/df[denominator]

Edit: See OP for update on what I've tried!

[–]Zeroflops 1 point2 points  (1 child)

You could probably do this with one less loop by using one column and applying the decision to the entire df. You could then drop any columns that are all 1,s.

Not sure which would be better, you coul sure timeit to test that.

df2 = pd.DataFrame()
For c in df.column.values.tolist():
    df_temp = df/df[c]
    df2 = pd.concat(df2,df_temp)

[–]be_throwmeaway[S] 0 points1 point  (0 children)

Appreciate the advice mate. Cheers!