I am trying to do the following task; I have a dataframe in python with N columns. For each pair of column, I want to create one single column with the ratio between the values of the second column and the previous one. I think I found the proper way to do it but I can't find the way to display the result into a new Dataframe. My input dataframe looks like this:
Name 1A 1B 2A 2B 3A 3B 536A 536B ...
name1 x1 x2 x3 x4 x5 x6 x7 x8
name2 ........
namN ........
So for each pair, let's take the first one for example, I want to create a column defined by 1B/1A, than one defined by 2B/2A ecc. This is the code that I tried:
l = []
for i in np.arange(0,536,2):
dic1={}
dic1[i+1] = df.iloc[:,i]/df.iloc[:,i+1]
l.append(dic1)
But after I tried:
pd.DataFrame(l)
I got a confused dataframe in which values of multiple columns are stored in the same cell. I report here the result.
I guess that is because I did not define the name of the columns that I created with the ratio, but I can't figure it out. Do you have any suggestions? Thank you!
