我想重新组织字典到面板数据格式,但出现错误

时间:2018-04-11 14:19:35

标签: python pandas dictionary

我的问题与Reorganize Dictionary非常相似 我的词典与上面提到的类似。所以我想复制一下。 但我必须错误地说“'' str' object不能解释为整数。我告诉你我的代码。

appended_data = pd.DataFrame()
for item in data:
    for ii in list(item.keys()):
        df = pd.DataFrame.from_dict(item[ii], orient='index')
        df['date'] = int(ii)
        df['code'] = item[ii]
        appended_data = appended_data.append(df)
dic = [appended_data.to_dict(orient='records')]
# [[{'comp': '삼성자산운용', 'name': 'KODEX 레버리지', 'base': '코스피 200', 'lstdt': '2010/02/22', 'tax': '배당소득세(보유기간과세)', 'earn': '55.59', 'bosu': '0.64', 'ocha': '3.87', 'gap': '-0.48', 'nav': '18,494', 'volt': '높음', 'bun': '주식-시장대표', 'repli': '실물', 'pdf': '', 'info': '보기', 'date': 20180102, 'code': 'A278240'}, {'comp': '미래에셋자산운용', 'name': 'TIGER 레버리지', 'base': '코스피 200', 'lstdt': '2010/04/09', 'tax': '배당소득세(보유기간과세)', 'earn': '57.98', 'bosu': '0.09', 'ocha': '3.66', 'gap': '-0.27', 'nav': '295', 'volt': '보통', 'bun': '주식-시장대표', 'repli': '실물', 'pdf': '', 'info': '보기', 'date': 20180102, 'code': 'A267770'}]

output = {}
for entry in dic:
    entry = entry.copy()
    date = entry.pop('date') #Here is the error
    code = entry.pop('code') #Here is the error
    output.setdefault(code, {})[data] = entry

非常感谢

1 个答案:

答案 0 :(得分:0)

最后,我找到了答案,但计算机需要大量内存。

import collections
import itertools
import pandas as pd

def expand_grid(data_dict):
  rows = itertools.product(*data_dict.values())
  return pd.DataFrame.from_records(rows, columns=data_dict.keys())

appended_data = pd.DataFrame()
for item in re_data2:
    for ii in list(item.keys()):
        df = pd.DataFrame.from_dict(item[ii], orient='index')
        df['date'] = ii
        df['code'] = item[ii]
        appended_data = appended_data.append(df)

dic = [appended_data.to_dict(orient='records')]
d = collections.defaultdict(dict)

for entry in dic:
    for item in entry:
        key1 = item['code']
        key2 = item['date']
        value = {item['comp'], item['name'], item['base'], item['lstdt'], item['tax'], item['earn'], \
                item['bosu'], item['ocha'], item['gap'], item['nav'], item['volt'], item['bun'], \
                item['repli'], item['pdf'], item['info']}
        d[key1][key2] = value
print(d)