美文网首页
python pandas 字典转数据框

python pandas 字典转数据框

作者: 像鸟一样飞过你的高山 | 来源:发表于2023-03-06 17:00 被阅读0次

    默认情况下,字典的键成为DataFrame的列

    data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
    pd.DataFrame.from_dict(data)
       col_1 col_2
    0      3     a
    1      2     b
    2      1     c
    3      0     d
    

    指定 orient='index' 以使用字典键作为DataFrame的行

    data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']}
    pd.DataFrame.from_dict(data, orient='index')
     0  1  2  3
    row_1  3  2  1  0
    row_2  a  b  c  d
    

    点击pandas.DataFrame.from_dict查看详细参数。

    相关文章

      网友评论

          本文标题:python pandas 字典转数据框

          本文链接:https://www.haomeiwen.com/subject/tdbmldtx.html