大熊猫Datarame到dict

时间:2018-03-02 20:37:55

标签: python pandas dictionary

我有一个数据框:

public class XmlEampleController : ApiController
{
    [HttpPost]
    [ActionName("MyOrderAction")]
    public HttpResponseMessage MyOrder([FromBody]MyOder order)
    {
        if (order != null)
        {
            return Request.CreateResponse<MyOder>(HttpStatusCode.Created, order);
        }
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }

[Serializable]
public partial class MyOder
{
    private string dataField;
    public string MyData
    {
        get
        {
            return this.dataField;
        }
        set
        {
            this.dataField = value;
        }
    }
}

}

我需要将A和C中的每一行转换为dict 我应该能够得到这个:

pd.DataFrame([[1,2,3],[111,222,333]], columns=['A', 'B', 'C'])

     A    B    C
0    1    2    3
1  111  222  333
2   11   22   33

到目前为止,我还未能找到如何选择应包含哪些列以及如何不包含标题

3 个答案:

答案 0 :(得分:2)

这是一种方式:

d = df.set_index('A')['C'].to_dict()

答案 1 :(得分:2)

来自zip

dict(zip(df.A,df.C))
Out[1073]: {1: 3, 11: 33, 111: 333}

更新

from collections import defaultdict
d = defaultdict(dict)
for _,x in df.iterrows():
    d[x['A']][x['B']] = x['C']


d
Out[74]: defaultdict(dict, {1: {2: 3}, 11: {22: 33}, 111: {222: 333}})

答案 2 :(得分:1)

谢谢你们!以下是有人想知道优化的不同之处:

%%timeit
d1 = df.set_index('A')['C'].to_dict()

[Out]: 2.46 ms ± 11.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)


%%timeit
d2 = dict(zip(df['A'],df['C']))

[Out]: 1.23 ms ± 12.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)