首页 > 解决方案 > 如何从字典创建熊猫数据框并将列表保存在一个单元格中

问题描述

我有一本像这样的字典:

dic = {'c': [14, 41],
       'i': '52983542076720',
       'p': 31.7,
       's': 100,
       't': 1611945588012261376,
       'x': 11}

我试过了

pd.DataFrame(dic) 

pd.DataFrame.from_dict(dic,, orient='columns', dtype=int, columns=None)

但是,它们都返回一个 2 行数据框,例如:

C 一世 p s X
0 14 52983542076720 31 100 1611945588012261376 11
1 41 52983542076720 31 100 1611945588012261376 11

我实际上想得到一个像这样的数据框:

C 一世 p s X
0 [14,41] 52983542076720 31 100 1611945588012261376 11

关于我应该怎么做才能得到结果的任何想法?

标签: pythonpandasdataframedictionary

解决方案


您可以pandas.Series改用:

pd.Series(dic).to_frame().T

输出:

          c               i     p    s                    t   x
0  [14, 41]  52983542076720  31.7  100  1611945588012261376  11

推荐阅读