Reputation: 25
I just started studying programming using python and I am trying to work out how to smartly perform divisions between columns.
I have a data frame as below: Data frame A
The numbers(e.g. 101) in the columns are subject IDs. I want to divide the values in the columns 101-A-5 over the values in the column 101-A-4 and store the answer in the data frame B: Data Frame B
I want to repeat the process for all the subject IDs. ( in the example I have included only three but in the real data there are 10)
To summarize, I want to do
{subject_id}-A-5 / {subject_id}-A-4
.
Is there a smart way to do this?
Upvotes: 2
Views: 45
Reputation: 17368
You can make a list of all subject_ids
and then apply the column division to each subject id
import random
import pandas as pd
df = pd.DataFrame({
"101-A-4":random.sample(range(0,100),100),
"101-A-5":random.sample(range(0,100),100),
"102-A-4":random.sample(range(0,100),100),
"102-A-5":random.sample(range(0,100),100)
})
data = {}
subject_ids = ["101","102"]
for i in subject_ids:
data[i] = df[f"{i}-A-5"]/df[f"{i}-A-4"]
new_df = pd.DataFrame(data)
Which gives:
101 102
0 0.516854 2.307692
1 0.683544 2.222222
2 0.289474 0.073529
3 1.812500 3.478261
4 2.800000 0.085714
.. ... ...
95 1.695652 0.666667
96 0.666667 2.714286
97 0.897059 0.222222
98 0.447368 1.540541
99 2.742857 0.444444
[100 rows x 2 columns]
Upvotes: 1