recherche

Maison  >  Questions et réponses  >  le corps du texte

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 Il y a quelques jours667

répondre à tous(3)je répondrai

  • 高洛峰

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

    Ceci peut être réalisé en utilisant pandas groupby, le code est pour votre référence

    # 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()

    répondre
    0
  • 高洛峰

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

    Organisez d'abord les cellules dans l'ordre, puis fusionnez les cellules.

    répondre
    0
  • 阿神

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

    Vous avez besoin d'une bibliothèque pour faire fonctionner Excel, telle que : openpyxl.

    Vérifiez ensuite comment remplir les données dans cette bibliothèque et comment fusionner des cellules.

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

    répondre
    0
  • Annulerrépondre