Home > Blockchain >  A simple maths operation on a CSV column using python [closed]
A simple maths operation on a CSV column using python [closed]

Time:09-22

So I have this CSV file that I need to plug into an API, but one of the columns is in Dollars, and I want to convert it to cents by multiplying by a 100

Here's the file

tripId,scooterId,userId,totalDuration,pickUpTime,dropOffTime,userLocationAtBooking.0,userLocationAtBooking.1,userLocationAtDropOff.0,userLocationAtDropOff.1,totalFare
18721,927,38579,45,2021-08-22 03:00:49,2021-08-22 03:45:39,24.76412,46.6493,24.76409833,46.64934,9.58
18722,434,38563,45,2021-08-22 03:01:16,2021-08-22 03:45:39,24.76412,46.64933333,24.76407,46.64933333,13.53
18723,876,38554,33,2021-08-22 03:05:57,2021-08-22 03:38:55,24.71392833,46.660645,24.7097,46.66272,0.67
18724,476,32291,65,2021-08-22 03:14:37,2021-08-22 04:18:56,24.77137833,46.64568667,24.7722,46.64523167,32.35

I only need to do the maths operation on the last column, "totalFare"

I'm still new to python, so any help would be highly appreciated.

CodePudding user response:

In case it was still unclear on how to perform the task here's an implementation using basic Python

with open('file.csv', 'r') as in_file, open('result.csv', 'w') as out_file: # Open input and output files
    out_file.write(next(in_file))                # Reads header from input file and writes to output file
    for line in in_file:                         # For each line in file (we are past header line)
        line = line.rstrip().split(',')          # Create list from comma separated string
        line[-1] = f'{100*float(line[-1]):.0f}'  # Convert last item in list to float and multiply by 100
                                                 # and convert foat to stringto 0 decimal places
            
        #line[-1] = '{:.0f}'.format(100*float(line[-1])) # Alternative to above line that doesn't use
                                                         # f-string (i.e. can use in earlier versions of Python)  
        
        out_file.write(','.join(line)   '\n')    # Output updated list as comma separated string
    

Output (using example input)

tripId,scooterId,userId,totalDuration,pickUpTime,dropOffTime,userLocationAtBooking.0,userLocationAtBooking.1,userLocationAtDropOff.0,userLocationAtDropOff.1,totalFare
18721,927,38579,45,2021-08-22 03:00:49,2021-08-22 03:45:39,24.76412,46.6493,24.76409833,46.64934,958
18722,434,38563,45,2021-08-22 03:01:16,2021-08-22 03:45:39,24.76412,46.64933333,24.76407,46.64933333,1353
18723,876,38554,33,2021-08-22 03:05:57,2021-08-22 03:38:55,24.71392833,46.660645,24.7097,46.66272,67
18724,476,32291,65,2021-08-22 03:14:37,2021-08-22 04:18:56,24.77137833,46.64568667,24.7722,46.64523167,3235

Alternative using Python Pandas module (much shorter solution)

import pandas as pd

df = pd.read_csv('file.csv')               # Read CSV file into Pandas DataFrame
df['totalFare'] *= 100                     # multiply total_fare by 100
df.to_csv('result.csv', index = False)     # Write to output file as csv
  • Related