Skip to content
Advertisement

Divide multiple columns in pandas

I’m working with the following table:

input_test input_test2 input_test3 ip_test ip_test2 ip_test3
ENSG00000000003.15 1 1 1 3 3 3
ENSG00000000457.14 2 2 2 1 1 1
ENSG00000000460.17 2 2 2 3 3 3
ENSG00000001036.14 3 3 3 4 4 4
ENSG00000001167.14 3 3 3 5 5 5

My goal is to make a new column called translational efficiency that divides the averaged ip columns by the averaged input columns, which I’m thinking should look like this:

input_test input_test2 input_test3 ip_test ip_test2 ip_test3 translational_efficiency
ENSG00000000003.15 1 1 1 3 3 3 3
ENSG00000000457.14 2 2 2 1 1 1 0.5
ENSG00000000460.17 2 2 2 3 3 3 1.5
ENSG00000001036.14 3 3 3 4 4 4 1.3
ENSG00000001167.14 3 3 3 5 5 5 1.6

Thus far, I’ve created a script with the following arguments:

JavaScript

Notice that the --ip_files and --input_files arguments take in multiple columns as a list that reflect the columns listed in the table.

I was hoping to do something like this:

JavaScript

However, this gives me the following error: ValueError: Wrong number of items passed 6, placement implies 1

Advertisement

Answer

you can grab columns by prefix using startswith string method

JavaScript

and calculate mean on axis=1 for those columns (for each row) and have a new column translational_efficiency by dividing these mean.

JavaScript

As you described in updated question and comment, i guess you can use df[ip_files] and df[input_files] directly as they have column names. You can ignore column name extraction part

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement