python 复杂表格合并
伊谢尔伦
伊谢尔伦 2017-04-18 10:28:30
0
3
672
[
{"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 如何实现这种复杂的表格排列

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

membalas semua(3)
小葫芦

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()
小葫芦

Susun sel mengikut urutan dahulu dan kemudian cantumkan sel.

阿神

Anda memerlukan perpustakaan untuk mengendalikan Excel, seperti: openpyxl.

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

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

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan