cari

Rumah  >  Soal Jawab  >  teks badan

python 复杂表格合并

[
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色1","数量":"1",},
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色2","数量":"20",},
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色3","数量":"3",},
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色4","数量":"4",},
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色5","数量":"5",},
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色6","数量":"6",},
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色7","数量":"30",},
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色8","数量":"8",},
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色9","数量":"9",},
{"ID":"1","订单号":"123456","用户名":"路人甲","产品":"XX用品","颜色":"色10","数量":"40",},
{"ID":"2","订单号":"456789","用户名":"路人乙","产品":"XXX用品","颜色":"红色","数量":"100",},
{"ID":"3","订单号":"123456789","用户名":"路人丙","产品":"XXXX用品","颜色":"金色","数量":"300",},
{"ID":"3","订单号":"987654321","用户名":"路人丙","产品":"XXXX用品","颜色":"紫色","数量":"100",},

]

需要转换表格为:

python3 如何实现这种复杂的表格排列

伊谢尔伦伊谢尔伦2786 hari yang lalu666

membalas semua(3)saya akan balas

  • 高洛峰

    高洛峰2017-04-18 10:30:30

    Ini boleh dicapai menggunakan panda groupby, kod ini untuk rujukan anda

    # coding: utf-8
    from __future__ import unicode_literals
    
    import pandas as pd
    lst = [
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色1", "数量": "1", },
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色2", "数量": "20", },
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色3", "数量": "3", },
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色4", "数量": "4", },
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色5", "数量": "5", },
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色6", "数量": "6", },
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色7", "数量": "30", },
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色8", "数量": "8", },
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色9", "数量": "9", },
        {"ID": "1", "订单号": "123456", "用户名": "路人甲", "产品": "XX用品", "颜色": "色10", "数量": "40", },
        {"ID": "2", "订单号": "456789", "用户名": "路人乙", "产品": "XXX用品", "颜色": "红色", "数量": "100", },
        {"ID": "3", "订单号": "123456789", "用户名": "路人丙", "产品": "XXXX用品", "颜色": "金色", "数量": "300", },
        {"ID": "3", "订单号": "987654321", "用户名": "路人丙", "产品": "XXXX用品", "颜色": "紫色", "数量": "100", }
    ]
    
    df = pd.DataFrame(lst)
    df["数量"] = df["数量"].apply(lambda x: int(x))
    df["备注"] = ''
    df1 = df.groupby(["ID","用户名","产品", '备注',"订单号","颜色"]).sum()
    
    #导出到excel
    df1.to_excel('test.xlsx')
    
    #输出html
    print df1.to_html()

    balas
    0
  • 高洛峰

    高洛峰2017-04-18 10:30:30

    Susun sel mengikut urutan dahulu dan kemudian cantumkan sel.

    balas
    0
  • 阿神

    阿神2017-04-18 10:30:30

    Anda memerlukan perpustakaan untuk mengendalikan Excel, seperti: openpyxl.

    Kemudian semak cara mengisi data dalam perpustakaan ini dan cara menggabungkan sel.

    http://openpyxl.readthedocs.i...

    balas
    0
  • Batalbalas