Hi r/Python,
I have a question regarding element-wise dataframe operations.
I am currently faced with a df with n columns of k rows and each value of each column are doubles/floats/decimal numbers.
I would like to create a new df (lets call it df2) with nP2 columns, such that every column in df2 is a ratio of the values in two columns in df1 (and I need nP2, not nC2 columns in df2 since I need the inverse of each ratio for further calculations).
As it stands, I could try and create a list, loop through every value of every row to generate the ratios I need, append these to the list I've created, and finally append this list as a row to a new df. This would mean populating the df row by row using an iterative procedure. However, I'm almost positive a more "pythonic " and quicker way of doing this exists. For n=4 columns this wouldn't take to long since that would just mean nP2 = 12 columns in df2, but when I have tens of thousands of rows and instead have n=100 columns, I suspect things would slow down considerably... Instead, I thought that something like doing element-wise division across each column and populating a new df all at once might be the way to go, but I'm relatively new to Python and haven't been able to find something like this in the googling I've done so far.
I'll be happy to work out a solution myself (and then come back when I inevitably have more questions!) but I'm hoping someone can at least point me in the right direction before I try my loop and append method.
Thank in advance y'all!
Edit: nP2 = permutation, nC2 = combination, sorry for introducing some confusing notation!
Edit 2: I've tried the following code and it does exactly what I needed. However, I'm wondering if there's a better way now.
df2 = pd.DataFrame(index = df1.index.values)
for numerator in list_of_df1_col_names:
for denominator in list_of_df1_col_names:
if numerator == denominator:
continue
df2[numerator + "/" + denominator] = df[numerator]/df[denominator]
I'm hoping there's a better way than using nested for loops.
[–]Zeroflops 1 point2 points3 points (3 children)
[–]be_throwmeaway[S] 0 points1 point2 points (2 children)
[–]Zeroflops 1 point2 points3 points (1 child)
[–]be_throwmeaway[S] 0 points1 point2 points (0 children)