Python_Pandas - Populating new row with condition

818 views python

I am trying to go through my Dataframe and create another row named 'Conversions'. I would like the "Conversions" row only to populate when the "Currency" row is "EUR" and pass over if it is not EUR Once an "EUR" currency has been identified, I would like to multiply it by the JPY object which is 2

Current DF looks like this:

Name     Currency     Amount  
a        EUR          12.00
b        USD          10.00
c        EUR           8.00
d        JPY          100.00
e        EUR          567.00

Once Executed, I would want the finished product to look like this:

Name     Currency     Amount  Conversions     
a        EUR          12.00      24.00
b        USD          10.00      20.00
c        EUR           8.00      16.00
d        JPY          100.00    200.00
e        EUR          567.00   1134.00

*the data is in a csv.
Please see my attempt below:

import csv import pandas as pd import numpy as np

JPY = (2)

df = pd.read_csv('df.csv', delimiter=";")

for i in df.iteritems():
    if df.loc[(df['Currency'] == 'EUR')]:
        df['Conversions']= (df.to_numeric(df['Amount']*(JPY)))

Shell Response - ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Thanks in advance for any assistance you can provide this newb.

answered question

use, choices, default) all i can help you with unless you post a dataframe

Thanks for the edit. Your expected output for conversions just is the amount multiplied by 2 just do df['Amount']*2 Nothing to do with the currency column

Hey Chris, Thanks for the response. I only want it to apply when the Currency is EUR only. The others should remain the same.

OK so you expect output is not correct then; however, I know what you are looking for

1 Answer


use no need to use a loop:

# your condition
conditions = [
    df['Currency'] == 'EUR'

# if condition is met
choices = [

# create a new column and a default value if no condition is met
# you can always change the default value to np.nan if you do not want a value
df['Conversions'] =, choices, default=df['Amount'])

    Name    Currency    Amount  Conversions
0   a            EUR    12.0    24.0
1   b            USD    10.0    10.0
2   c            EUR    8.0     16.0
3   d            JPY    100.0   100.0
4   e            EUR    567.0   1134.0

posted this

Have an answer?


Please login first before posting an answer.