python词典到pandas数据框有多个列

时间:2016-12-16 21:03:53

标签: python pandas dictionary dataframe

我有以下python字典:

d= {'data'      : Counter({ 'important' : 2,
                        'very'      : 3}),
    'analytics' : Counter({ 'boring'    : 5,
                        'sleep'     : 3})
    }

我希望将其保存为以下格式的pandas数据帧:

category  | word      | count
  data    | important | 2
  data    | very      | 3
analytics | boring    | 5
analytics | sleep     | 3

我尝试了以下操作,但没有一个工作

df = pd.DataFrame(d.items()) 

df = pd.DataFrame.from_dict(d, orient='index').reset_index()

df = pd.DataFrame(data)

1 个答案:

答案 0 :(得分:7)

您可以使用stack

df = pd.DataFrame(d).stack().reset_index()
df.columns = ['word','category','count']
print(df)
        word   category  count
0     boring  analytics    5.0
1  important       data    2.0
2      sleep  analytics    3.0
3       very       data    3.0
df = pd.DataFrame.from_dict(d, orient='index').stack().reset_index()
df.columns = ['category','word','count']
print(df)

    category       word  count
0  analytics     boring    5.0
1  analytics      sleep    3.0
2       data  important    2.0
3       data       very    3.0

嵌套列表理解的另一种解决方案:

df = pd.DataFrame([(key,key1,val1) for key,val in d.items() for key1,val1 in val.items()])
df.columns = ['category','word','count']
print(df)
    category       word  count
0  analytics     boring      5
1  analytics      sleep      3
2       data  important      2
3       data       very      3