将pandas数据帧转换为列表的字典

时间:2018-06-15 17:35:02

标签: python-3.x list pandas dictionary

我有以下问题。我有一个从 pandas.read_sql 填充的pandas数据框。

数据框如下所示:

{'A':[70,23,67], 'B':[08,06,88,09], 'C':[40], 'D':[87,11]}

我需要将它转换为这样的列表字典:

if (property.PropertyType.IsEnum)
{
    // At this point we know that the property is an enum.
    // Add the EnumToStringConverter converter to the property so that
    // the value is stored in the database as a string instead of number 
    modelBuilder.Entity(modelType)
        .Property(property.Name)
        .HasConversion<string>(); // <--

    continue;
}

我已经尝试了2个小时,现在我没有想法。我想我错过了一些非常简单的事情。

1 个答案:

答案 0 :(得分:5)

使用groupbyto_dict

df.groupby('PERSON').GRADES.apply(list).to_dict()
Out[286]: {'A': [70, 23, 67], 'B': [8, 6, 88, 9], 'C': [40], 'D': [87, 11]}
相关问题