Aravinth
Aravinth

Reputation: 91

Calculated result is not added to the existing csv

I tried to write the output to the csv file as a new column. I can convert and print the date and time but I cannot write it to the same csv as in the column "Arrival_time". The code runs fine without error but there is no values in the csv column "Arrival time".

Where am I going wrong?

    import pandas as pd

df = pd.read_csv ('C:/New.csv',sep=';')
print (df)

import datetime
import csv

#for i in range(0,len(df)):
    #row= df.iloc[i]['arrival_unix_seconds']
    #a = int(row)
    #date = datetime.datetime.utcfromtimestamp(a)
    #targetDate = date.strftime("%Y-%m-%d %H:%M:%S")
    #print(targetDate)
    
df['arrival_unix_seconds'] = df.arrival_unix_seconds.apply(lambda x: \
    (datetime.datetime.utcfromtimestamp(int(x))).strftime("%Y-%m-%d %H:%M:%S"))

print(df.arrival_unix_seconds)

with open("New.csv", "a") as inputfile:

        title = ["id", "space_id", "arrival_unix_seconds", "departure_unix_seconds", "xml_id", "Arrival_time"]
        writer = csv.DictWriter(inputfile, delimiter=';', fieldnames = title)
        writer.writerow({"Arrival_time": df.arrival_unix_seconds})

Upvotes: 1

Views: 58

Answers (1)

Ank
Ank

Reputation: 1714

Since you are already using pandas and creating a dataframe, simply do below to calculate Arrival_time and write it back to your existing csv:

import pandas as pd
import datetime

df = pd.read_csv('C:/Master thesis/Sensor/parking_operations_berlin_hannover/New.csv', sep=';')
    
df['Arrival_time'] = df.arrival_unix_seconds.apply(lambda x: (datetime.datetime.utcfromtimestamp(int(x))).strftime("%Y-%m-%d %H:%M:%S"))
    
df.to_csv('C:/Master thesis/Sensor/parking_operations_berlin_hannover/New.csv', sep=';', index=False)

Upvotes: 1

Related Questions