Emmanuel Gonzales II
Emmanuel Gonzales II

Reputation: 37

How to read csv file with date as one of the data?

These are my data both in excel and in csv files:

Date,Time,Product_Type 2015-01-02,02:29:45 PM,Cards

I've tried this code below and it works well with the excel file but not in CSV file.

import numpy as np
import pandas as pd

df = pd.read_excel('file.xlsx')
print(df.head())

My code in reading the csv file is almost same from the above code but I am getting an error. Please help.

import numpy as np
import pandas as pd
import datetime

df = pd.read_csv('file.csv', index_col='Date', parse_dates=True)
print(df.head())

ERROR Message: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa4 in position 2: invalid start byte

Upvotes: 1

Views: 1794

Answers (2)

Emmanuel Gonzales II
Emmanuel Gonzales II

Reputation: 37

I have modified and removed the column names on my csv file and used this code below. It works!

CSV File Data

    2015-01-02,02:29:45 PM,Cards
    2015-01-02,05:16:15 PM,Cards
    2015-01-02,05:48:46 PM,Cards
    2015-01-02,03:18:34 PM,Cards
    2015-01-02,05:22:55 PM,Cards

My code:

df = pd.read_csv('datacsv.csv', sep=',', parse_dates=[0], header=None,
                         names=['Date', 'Time', 'Value'])

print (df.head())

  Date         Time  Value
0 2015-01-02  02:29:45 PM  Cards
1 2015-01-02  05:16:15 PM  Cards
2 2015-01-02  05:48:46 PM  Cards
3 2015-01-02  03:18:34 PM  Cards
4 2015-01-02  05:22:55 PM  Cards

Thanks for your responses guys!

Upvotes: 1

yarz-tech
yarz-tech

Reputation: 274

I'm not sure exactly what you plan to do with the data once its pulled from the file so if you need a different format or something let me know.

I'm assuming you'll always be working with a CSV for this code. The code below simply opens your file and for every line, splits by the commas, and appends to a list (each index being a line of code) for good organization.

File = open("Filename.csv","r")

Data = []
for lines in File:
    Data.append([lines.split(",")])
'[[Date,Time,Product Type, Date,Time,Cards],[Date2,,,],,,]

Upvotes: 0

Related Questions