maslak
maslak

Reputation: 1245

converter argument depends on another column value

In my script, I would like to read some csv file and at the same time convert input values. But the value of one column depends on another column's (this column is not going to be converted) value. Is there any way to achieve that in read_csv or do I have to change it after csv is read?

file.csv

date        total        percentage
03/25/2017  100          1%
04/15/2016  200          6%

expected output

date        total        success
03/25/2017  100          1
04/15/2016  200          12
def convert_succes(percentage):
    # is there any way to pass an 'total' value to this function?
    return percentage / 100

names = ['date', 'total', 'success']

converters = {
        'date': pandas.to_datetime,
        'success': convert_succes,
    }

input_report = pandas.read_csv('file.csv', names=names, converters=converters)

Upvotes: 1

Views: 184

Answers (1)

anky
anky

Reputation: 75080

Strip the string % and convert to float then multiply:

df['success']=df.total*df.percentage.str.rstrip('%').astype('float') / 100.0
print(df)

         date  total percentage  success
0  03/25/2017    100         1%      1.0
1  04/15/2016    200         6%     12.0

To convert it from string to float at the time of reading from a file use the below from here:

def p2f(x):
    return float(x.strip('%'))/100

 pd.read_csv(file, sep='whatever',converters={'percentage':p2f})

Upvotes: 1

Related Questions