我的数据是一个字典,里面有一个字典列表。
data = {'John': [{'date': u'2018-01-21', 'name' : u'John', 'fieldname': u'lunch', 'fieldvalue': u'10'}, {'date': u'2018-01-21', 'name' : u'John', 'fieldname': u'dinner', 'fieldvalue': u'9'}], 'Maria': [{'date': u'2018-01-21', 'name' : u'Maria', 'fieldname': u'lunch', 'fieldvalue': u'10'}, {'date': u'2018-01-21', 'name' : u'Maria', 'fieldname': u'dinner', 'fieldvalue': u'9'}], 'Roger': [{'date': u'2018-01-21', 'name' : u'Roger', 'fieldname': u'lunch', 'fieldvalue': u'10'}, {'date': u'2018-01-21', 'name' : u'Roger', 'fieldname': u'dinner', 'fieldvalue': u'9'}], 'Peter': [{'date': u'2018-01-21', 'name' : u'Peter', 'fieldname': u'lunch', 'fieldvalue': u'10'}, {'date': u'2018-01-21', 'name' : u'Peter', 'fieldname': u'dinner', 'fieldvalue': u'9'}]}
我要做的是在每个data.key中有一个字典列表的数据框.key
我从: mydf = df(data.keys())
mydf
Out[18]:
0
0 John
1 Roger
2 Peter
3 Maria
我尝试在每个dict.key中加入df.data
mydf(df(data))
Traceback (most recent call last):
File "<ipython-input-43-5c9743ed39d1>", line 1, in <module>
mydf(df(data))
TypeError: 'DataFrame' object is not callable
我的最终目标是定义一个函数,为每个键内部的datatrame写一个csv。
我的问题是如何将数据帧放在字典键中,对于每个键对应的数据帧。
要存储在csv中的每个dict.key的所需输出:
name date fieldname fieldvalue
Roger 20180301 lunch 12
Roger 20180301 dinner 12
Roger 20180301 lunch 12
Roger 20180301 dinner 12
答案 0 :(得分:1)
试试这个:
for ii = 1:5
out=function1(ii);
if out,continue;end
end
function out=function1(in)
if in,out=1;else,out=0;disp('hi');end
end
DataFrame结果:
import pandas as pd
data = {
'John': [{'date': u'2018-01-21', 'name' : u'John', 'fieldname': u'lunch', 'fieldvalue': u'10'},
{'date': u'2018-01-21', 'name' : u'John', 'fieldname': u'dinner', 'fieldvalue': u'9'}],
'Maria': [{'date': u'2018-01-21', 'name' : u'Maria', 'fieldname': u'lunch', 'fieldvalue': u'10'},
{'date': u'2018-01-21', 'name' : u'Maria', 'fieldname': u'dinner', 'fieldvalue': u'9'}],
'Roger': [{'date': u'2018-01-21', 'name' : u'Roger', 'fieldname': u'lunch', 'fieldvalue': u'10'},
{'date': u'2018-01-21', 'name' : u'Roger', 'fieldname': u'dinner', 'fieldvalue': u'9'}],
'Peter': [{'date': u'2018-01-21', 'name' : u'Peter', 'fieldname': u'lunch', 'fieldvalue': u'10'},
{'date': u'2018-01-21', 'name' : u'Peter', 'fieldname': u'dinner', 'fieldvalue': u'9'}]}
new_data = []
for key, value in data.items():
new_data.extend(value)
df = pd.DataFrame(new_data, columns = ['name', 'date', 'fieldname', 'fieldvalue'])
有了这个,将pandas数据帧写入csv应该是小菜一碟。
name date fieldname fieldvalue
0 John 2018-01-21 lunch 10
1 John 2018-01-21 dinner 9
2 Maria 2018-01-21 lunch 10
3 Maria 2018-01-21 dinner 9
4 Roger 2018-01-21 lunch 10
5 Roger 2018-01-21 dinner 9
6 Peter 2018-01-21 lunch 10
7 Peter 2018-01-21 dinner 9
答案 1 :(得分:0)
使用numpy中的concatenate
函数,可以在一行中完成。
# convert dictionary to data frame
df = pd.DataFrame(list(np.concatenate(list(data.values()))))
# write the file
df.to_csv('output.csv', index=False)
# print
print(df)
date fieldname fieldvalue name
0 2018-01-21 lunch 10 John
1 2018-01-21 dinner 9 John
2 2018-01-21 lunch 10 Maria
3 2018-01-21 dinner 9 Maria
4 2018-01-21 lunch 10 Roger
5 2018-01-21 dinner 9 Roger
6 2018-01-21 lunch 10 Peter
7 2018-01-21 dinner 9 Peter
答案 2 :(得分:0)
使用reduce
解包data
:
from functools import reduce # if python2, delete this line.
df = pd.DataFrame(reduce(lambda x, y: x + y, list(data.values())))
df.to_csv("data.csv", index=False)
答案 3 :(得分:0)
for (fieldname, fieldvalue) in data.iteritems():
print fieldname
print df(fieldvalue)
John
date fieldname fieldvalue name
0 2018-01-21 lunch 10 John
1 2018-01-21 dinner 9 John
Roger
date fieldname fieldvalue name
0 2018-01-21 lunch 10 Roger
1 2018-01-21 dinner 9 Roger
Peter
date fieldname fieldvalue name
0 2018-01-21 lunch 10 Peter
1 2018-01-21 dinner 9 Peter
Maria
date fieldname fieldvalue name
0 2018-01-21 lunch 10 Maria
1 2018-01-21 dinner 9 Maria