0

I have the following code:

    data_keys = data.keys()
    for i in range(1,len(data_keys)):
        multiplier = specie_name_and_initial_values_dict[data_keys[i]]
        data[data_keys[i]]=map(lambda x: multiplier*x, data[data_keys[i]])
    return data

data_keys is a list of keys derived from a pandas dataframe i.e.

Index([u'Time', u'Cyp26_G_R1', u'Cyp26_G_rep1'], dtype='object')

data is a pandas data frame, i.e.:

    Time  Cyp26_G_R1  Cyp26_G_rep1
0      0         0.0          14.0
1    300         0.0          10.0
2    600         1.0           1.5
3    900         2.0           2.0
4   1200         2.5           1.0    
5   1500         4.0           0.9
6   1800         2.0           2.0
7   2100         2.5           2.5
8   2400         5.0           2.5
9   2700         6.5           0.5
10  3000         6.0           0.4
11  3300         5.5           0.5
12  3600         1.5           1.5    

and specie_name_and_initial_values_dict is a dictionary with 35 keys, one value in each key and two of those keys have the same name as the column headers in the data frame above (not 'Time').

I'm trying to multiply the two data columns of my data frame by each of the two values that I get from the 'specie_name_and_initial_values_dict[data_keys[i]]' within the loop.

I cannot paste my current output because stack overflow doesn't like the format but what is currently happening is the multiplication is not occurring and the numbers are iteratively being spliced i.e. first element = 240.8856716, second = 240.8856716240.8856716 and this continues.

This is obviously not what I want. Does anybody know a way to do this?

2 Answers 2

2

You want to multiply a column in your dataframe with a value from a dictionary, where the key is the column name ? Use the mul function:

In [18]: df
Out[18]: 
    Time  Cyp26_G_R1  Cyp26_G_rep1
0      0         0.0          14.0
1    300         0.0          10.0
2    600         1.0           1.5
3    900         2.0           2.0
4   1200         2.5           1.0
5   1500         4.0           0.9
6   1800         2.0           2.0
7   2100         2.5           2.5
8   2400         5.0           2.5
9   2700         6.5           0.5
10  3000         6.0           0.4
11  3300         5.5           0.5
12  3600         1.5           1.5

In [19]: d
Out[19]: {'Cyp26_G_R1': 100, 'Cyp26_G_rep1': 10}

In [20]: key = 'Cyp26_G_rep1'

We are going to multiply column 'Cyp26_G_rep1' with value of the same key form the dictionary d -

In [21]: df[key] = df[key].mul(d[key])

In [22]: df
Out[22]: 
    Time  Cyp26_G_R1  Cyp26_G_rep1
0      0         0.0           140
1    300         0.0           100
2    600         1.0            15
3    900         2.0            20
4   1200         2.5            10
5   1500         4.0             9
6   1800         2.0            20
7   2100         2.5            25
8   2400         5.0            25
9   2700         6.5             5
10  3000         6.0             4
11  3300         5.5             5
12  3600         1.5            15
Sign up to request clarification or add additional context in comments.

Comments

1

I was searching everywhere trying accomplish the exact same task. I found a less than ideal solution and just recently came across this solution. I took the solution provided by fixxer and built upon it. I'm new to StackOverflow so for now, since I don't want to mess up the original answer, I will provide a second answer.

In my solution I needed to keep the original dataframe as is, so I created a copy. Also I had numerous columns and key:value combos by which to apply the mul() function. So I looped through the dictionary in this way:

df2 = df.copy()
for d_key in d.keys():
    key = d_key
    df2[key] = df2[key].mul(d[key])

This worked nicely for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.