【问题标题】:Convert pandas DataFrame to dictionary using repeating cell values as keys使用重复单元格值作为键将 pandas DataFrame 转换为字典
【发布时间】:2019-10-28 09:32:11
【问题描述】:

我有一个这样的数据框:

Sol Col1    v1  Col2   v2    Col3  v3   Col4    v4  
1   Y_1_1   0   Y_1_2   1   Y_1_3   0   Y_1_4   0       
2   Y_1_1   0   Y_1_2   1   Y_1_3   0   Y_1_4   0       
3   Y_1_1   0   Y_1_2   0   Y_1_3   0   Y_1_4   0       
4   Y_1_1   0   Y_1_2   0   Y_1_3   1   Y_1_4   0   
5   Y_1_1   0   Y_1_2   0   Y_1_3   1   Y_1_4   0   
6   Y_1_1   0   Y_1_2   0   Y_1_3   0   Y_1_4   0       
7   Y_1_1   0   Y_1_2   1   Y_1_3   0   Y_1_4   0       
8   Y_1_1   0   Y_1_2   0   Y_1_3   0   Y_1_4   1       
9   Y_1_1   0   Y_1_2   1   Y_1_3   0   Y_1_4   0

我想把它转换成这样的字典:

dic = {1: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0},
       2: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0},
       ...}

我想知道我应该用 str 变量(Y_1_1Y_1_2 等)替换列 v1v2v3 的标题,然后删除带有变量名称的列( col1, col2, ...)。

我找到了一些将数据框转换为字典的示例,但如果我没记错的话,它们中的任何一个都无助于解决我的问题。

有没有一种pythonic方法来进行这种转换?

【问题讨论】:

    标签: python pandas dataframe dictionary


    【解决方案1】:

    如果col1colN列中的值相同,则可以使用:

    #create index by `Sol` column
    df = df.set_index('Sol')
    
    #select first row, shift and create dictionary
    d = df.iloc[0].shift().to_dict()
    
    #select each `v1` column by indexing, rename columns and convert to dict
    out = df.iloc[:, 1::2].rename(columns=d).to_dict('index')
    print (out)
    
    {1: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0},
     2: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0}, 
     3: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 0, 'Y_1_4': 0}, 
     4: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 1, 'Y_1_4': 0}, 
     5: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 1, 'Y_1_4': 0}, 
     6: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 0, 'Y_1_4': 0}, 
     7: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0}, 
     8: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 0, 'Y_1_4': 1}, 
     9: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0}}
    

    如果 col1colN 列中的值可能不同,则使用字典推导和 zip 对和 unpair 值:

    d = {k: dict(zip(list(v.values())[::2], list(v.values())[1::2])) 
           for k, v in df.set_index('Sol').to_dict('index').items()}
    print (d)
    
    {1: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0}, 
     2: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0},
     3: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 0, 'Y_1_4': 0}, 
     4: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 1, 'Y_1_4': 0}, 
     5: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 1, 'Y_1_4': 0}, 
     6: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 0, 'Y_1_4': 0}, 
     7: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0}, 
     8: {'Y_1_1': 0, 'Y_1_2': 0, 'Y_1_3': 0, 'Y_1_4': 1}, 
     9: {'Y_1_1': 0, 'Y_1_2': 1, 'Y_1_3': 0, 'Y_1_4': 0}}
    

    【讨论】:

      【解决方案2】:

      我可以通过以下方式为您提供值作为键:

      df.drop(['Sol'], axis=1).transpose().reset_index(drop=True).to_dict()
      

      导致

      {0: {0: 'Y_1_1', 1: 0, 2: 'Y_1_2', 3: 1, 4: 'Y_1_3', 5: 0, 6: 'Y_1_4', 7: 0},
       1: {0: 'Y_1_1', 1: 0, 2: 'Y_1_2', 3: 1, 4: 'Y_1_3', 5: 0, 6: 'Y_1_4', 7: 0},
       2: {0: 'Y_1_1', 1: 0, 2: 'Y_1_2', 3: 0, 4: 'Y_1_3', 5: 0, 6: 'Y_1_4', 7: 0}, ...
      

      这对你有用吗?

      【讨论】:

        猜你喜欢
        • 2021-11-08
        • 1970-01-01
        • 2017-12-26
        • 2014-12-30
        • 2019-10-27
        • 2019-12-10
        • 2021-12-09
        相关资源
        最近更新 更多