Home > Software design >  Import data in a csv file that is structured as list
Import data in a csv file that is structured as list

Time:08-28

I'm trying to import data into a pandas dataframe. The file type is 'csv' but the data in the file is structured as a python list. The below code is only returning the column headers. Any suggestions? What am I doing wrong?

import pandas as pd

data_path = pd.read_csv(r'C:\Users\john_smith\file_name.csv')
df = pd.DataFrame(data_path, columns=["article_id","author_id","viewer_id","view_date"])
df

An example of the data in the file is below. There aren't any headers in the file.

[[126,17,62,"2019-07-02"],[149,42,22,"2019-06-23"],[138,39,33,"2019-07-26"]]

Example of what is returned is below:

enter image description here

CodePudding user response:

It's really not clear, but if you have a file that literally looks like:

file.csv

[[126,17,62,"2019-07-02"],[149,42,22,"2019-06-23"],[138,39,33,"2019-07-26"]]

We can attempt to read that with ast.literal_eval

from ast import literal_eval

with open('file.csv') as f:
    data = literal_eval(f.read())

print(data)
print(type(data))

# Output:

[[126, 17, 62, '2019-07-02'], [149, 42, 22, '2019-06-23'], [138, 39, 33, '2019-07-26']]
<class 'list'>

Now we can work with pandas:

df = pd.DataFrame(data, columns=["article_id","author_id","viewer_id","view_date"])
print(df)

# Output:

   article_id  author_id  viewer_id   view_date
0         126         17         62  2019-07-02
1         149         42         22  2019-06-23
2         138         39         33  2019-07-26
  • Related