简体   繁体   中英

pandas list of dicts dataframe

I have turned a list of dicts into a dataframe using this:

import pandas as pd
d = pd.read_csv('/path/to/file.csv')
res_df = pd.DataFrame(d)
res_df.head()

Pandas did not interpret the format successfully, I'm guessing bc there were no quotes around the keys of the dict. It looked like this:

[{location:'playroom',state:'NY',zip:10011},{..}]

As a workaround, I stripped out "'","{}", and "[]", to make the file standard csv. However, when I call the names argument from pd.read_csv, I have two issues: 1 - the names columns are blank, and 2 - I end up with a dataframe that is 1 row with thousands of columns. res_df.transpose() did not work.

If my csv has no header row, and assuming it has the same number of fields for each record , why is it that I can't give pandas my column names, and create new dataframe rows based on these arguments/instructions?

What is the quicker/better way to do this?

*Update: here is a snippet of the csv file:

websitedotcomcom/,Jim,jim@testdotcom,777-444-5555,Long City, NY,1,http://document-url,,another_field,,,true,12 Feb 2015 (18:17),http://website.com/,Jim,jim@test.com,777-444-5555,Long City, NY,1,http://document-url,,another_field,,,true,12 Feb 2015 (18:17)

This looks like JSON rather than CSV. You should use pandas read_json method .

df = pd.read_json('/path/to/file.json')

Note: that it is sensitive to valid json, say you may have to do some string manipulation (eg replacing ' with " ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM