pandas dicts数据帧列表

时间:2015-09-17 20:54:58

标签: python pandas dataframe

我使用以下方法将一个dicts列表转换为数据框:

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

Pandas没有成功解释格式,我猜bc在dict的键周围没有引号。它看起来像这样:

[{位置: '娱乐室',状态: 'NY',邮编:10011},{..}]

作为一种解决方法,我删除了“'”,“{}”和“[]”,以使文件标准为csv。但是,当我从pd.read_csv调用 names 参数时,我有两个问题:1 - 名称列为空白,2 - 我最终得到的数据帧为1行,包含数千列。 res_df.transpose()无效。

如果我的csv没有标题行,并且假设每个记录的字段数相同,为什么我不能给pandas我的列名,并创建新的数据帧行基于这些论点/指示?

更快/更好的方法是什么?

*更新:这是csv文件的片段:

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)

1 个答案:

答案 0 :(得分:0)

这看起来像JSON而不是CSV。您应该使用pandas read_json method

./term/lua.trm:      t_num = luaL_checkint(L, 1);
./term/lua.trm:      t_num = luaL_checkint(L, 1);
./term/lua.trm:  token_cnt = luaL_checkinteger(L, 1);

注意: 对有效的json敏感,假设您可能需要进行一些字符串操作(例如将df = pd.read_json('/path/to/file.json') 替换为')。