【问题标题】:How to convert rows in DataFrame in Python to dictionaries如何将 Python 中 DataFrame 中的行转换为字典
【发布时间】:2015-09-28 05:33:09
【问题描述】:

例如,我现在有 DataFrame

id score1   score2  score3  score4  score5
1  0.000000     0.108659    0.000000    0.078597    1
2  0.053238     0.308253    0.286353    0.446433    1
3  0.000000     0.083979    0.808983    0.233052    1

我想把它转换成

id scoreDict
1  {'1': 0, '2': 0.1086, ...}
2  {...}
3  {...}

无论如何要这样做?

【问题讨论】:

    标签: python dictionary pandas


    【解决方案1】:

    对于像我这样提出这个问题但希望执行以下操作的其他人: 逐行创建字典,根据相邻列的值映射一列。

    这是我们的映射表:

      Rating    y
    0  AAA      19
    1  AA1      18
    2  AA2      17
    3  AA3      16
    4  A1       15
    5  A2       14
    6  A3       13
          ...
    19 D       0
    

    在:

    import pandas as pd
    df_map.set_index('y')
    dict_y = df_map['Rating'].to_dict()
    

    输出:

    {19: 'AAA',
     18: 'AA1',
     17: 'AA2',
     16: 'AA3',
     15: 'A1',
     14: 'A2',
     13: 'A3',
     12: 'BBB1',
     11: 'BBB2',
     10: 'BBB3',
     9: 'BB1',
     8: 'BB2',
     7: 'BB3',
     6: 'B1',
     5: 'B2',
     4: 'B3',
     3: 'CCC1',
     2: 'CCC2',
     1: 'D'}
    

    【讨论】:

    • 对我来说,此解决方案无需致电 transpose 即可工作。当您转置时,您的Rating(列)成为(唯一)行,因此df_map['Rating'] 在我的(非常相似的)df 中返回错误
    【解决方案2】:
    df = pd.DataFrame({'col1': [1, 2],
                       'col2': [0.5, 0.75]},
                       index=['row1', 'row2'])
    df
          col1  col2
    row1    1   0.50
    row2    2   0.75
    
    df.to_dict(orient='index')
    {'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}}
    

    【讨论】:

      【解决方案3】:

      我认为下面的代码将为您提供您正在寻找的格式的数据框。它还允许您选择任何列作为索引

      import pandas as pd
      
      #IMPORT YOUR DATA
      #Any other way to import data can also be used. I saved it in .csv file 
      df=pd.read_csv('dftestid.csv')
      print("INITIAL DATAFRAME")
      print(df)
      print()
      
      #Convert Data Frame to Dictionary (set_index method allows any column to be used as index)
      df2dict=df.set_index('id').transpose().to_dict(orient='dict')
      
      
      #Convert Dictionary to List with 'score' replaced
      dicttolist=[[k,{int(k1.replace('score','')):v1 for k1,v1 in v.items()}] for k,v in df2dict.items()]
      
      #"Create the new DataFrame"
      
      df2=pd.DataFrame(dicttolist,columns=['id', 'scoreDict'])
      print("NEW DATAFRAME")
      print(df2)
      
      
      OUT:
      INITIAL DATAFRAME
         id    score1    score2    score3    score4  score5
      0   1  0.000000  0.108659  0.000000  0.078597       1
      1   2  0.053238  0.308253  0.286353  0.446433       1
      2   3  0.000000  0.083979  0.808983  0.233052       1
      
      NEW DATAFRAME
         id                                          scoreDict
      0   1  {1: 0.0, 2: 0.108659, 3: 0.0, 4: 0.078597, 5: ...
      1   2  {1: 0.053238, 2: 0.308253, 3: 0.286353, 4: 0.4...
      2   3  {1: 0.0, 2: 0.083979, 3: 0.808983, 4: 0.233052...
      

      【讨论】:

        【解决方案4】:
        import pandas as pd
        
        # your df
        # =========================
        print(df)
        
           id  score1  score2  score3  score4  score5
        0   1  0.0000  0.1087  0.0000  0.0786       1
        1   2  0.0532  0.3083  0.2864  0.4464       1
        2   3  0.0000  0.0840  0.8090  0.2331       1
        
        # to_dict
        # =========================
        df.to_dict(orient='records')
        
        Out[318]: 
        [{'id': 1.0,
          'score1': 0.0,
          'score2': 0.10865899999999999,
          'score3': 0.0,
          'score4': 0.078597,
          'score5': 1.0},
         {'id': 2.0,
          'score1': 0.053238000000000001,
          'score2': 0.308253,
          'score3': 0.28635300000000002,
          'score4': 0.44643299999999997,
          'score5': 1.0},
         {'id': 3.0,
          'score1': 0.0,
          'score2': 0.083978999999999998,
          'score3': 0.80898300000000001,
          'score4': 0.23305200000000001,
          'score5': 1.0}]
        

        【讨论】:

        • 值得为未来的读者补充一点, orient 参数根据您要如何拆分 DataFrame 采用几个不同的值——关于此方法的 pandas 文档可以在 pandas.pydata.org/docs/reference/api/… 找到
        猜你喜欢
        • 2016-10-16
        • 2021-12-23
        • 2020-08-27
        • 2016-05-26
        • 2020-08-06
        • 1970-01-01
        • 2019-02-06
        • 2022-06-29
        • 2021-06-11
        相关资源
        最近更新 更多