python 浣跨敤pandas搴撳垎鏋愬姞鐝暟鎹�

鍥犱负姣忎釜鏈堜細鍒嗘瀽閮ㄩ棬鍐呴儴鍚勪釜灏忓洟闃熺殑骞冲潎鍔犵彮鏃堕暱锛屼互璇勪及澶у鐨勫伐浣滈噺锛屾瘡娆℃墜宸ュ垎鏋愮函绮规槸閲嶅閫昏緫宸ヤ綔锛屽仛澶氫簡涔嬪悗渚胯寰楁灟鐕ユ棤鍛炽�備簬鏄兂鍒颁簡鍒╃敤python鍐欎竴涓剼鏈潵瑙f斁鑷繁銆�

#coding:utf-8

from openpyxl聽 import Workbook

from openpyxl聽 import load_workbook

from pandas import Series,DataFrame

import pandas as pd

import numpy as np,pandas as pd

import xlrd

df = pd.read_excel(r'E:\pythonstudy\excel\data\sample.xlsx','Worksheet',encoding='gbk')

#print(df[u'鍔犵彮鏃堕暱(鍒�)'])

#df01 = pd.read_excel(r'E:\pythonstudy\excel\data\9.et','Worksheet',encoding='gbk')

total_OT = df[u'鍔犵彮鏃堕暱(鍒�)'].sum()聽 #璁$畻鍔犵彮鏃堕暱鍒楃殑鎬诲拰

print(total_OT)

df_name = df[u'濮撳悕'].drop_duplicates() #鍓旈櫎濮撳悕鐨勯噸澶嶆暟鎹�

df_name_total = df_name.count()聽 #缁熻閮ㄩ棬鎬讳汉鏁�

print('total_num:',df_name_total)

team_dz_cp01 = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯涓�缁�']聽 #鎸夊皬缁勭瓫閫夋暟鎹�

team_dz_cp01_OT = team_dz_cp01[u'鍔犵彮鏃堕暱(鍒�)'].sum() #浜у搧娴嬭瘯涓�缁勫姞鐝椂闀跨粺璁�

print(team_dz_cp01_OT)

name_dz_cp01 = team_dz_cp01[u'濮撳悕'].drop_duplicates() #XX娴嬭瘯涓�缁勬寜濮撳悕鍓旈櫎閲嶅鏁版嵁

name_dz_cp01_num = name_dz_cp01.count()聽 #缁熻XX娴嬭瘯涓�缁勪汉鏁�

print(name_dz_cp01_num)

team_dz_cp02 = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯浜岀粍'] #鎸夊皬缁勭瓫閫夋暟鎹�

team_dz_cp02_OT = team_dz_cp02[u'鍔犵彮鏃堕暱(鍒�)'].sum()聽 #绛涢�夊嚭鐨勫皬缁勬暟鎹腑锛屾寜鍔犵彮鏃堕暱鍒楁眰鍜岋紝缁熻鍔犵彮鏃堕暱

print(team_dz_cp02_OT)

name_dz_cp02 = team_dz_cp02[u'濮撳悕'].drop_duplicates()

name_dz_cp02_num = name_dz_cp02.count()聽 #缁熻XX娴嬭瘯浜岀粍浜烘暟

print(name_dz_cp02_num)

team_dz_yy = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯缁�']

team_dz_yy_OT = team_dz_yy[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_dz_yy_OT)

name_dz_yy = team_dz_yy[u'濮撳悕'].drop_duplicates()

name_dz_yy_num = name_dz_yy.count() - 1聽 聽 #缁熻XX娴嬭瘯缁勪汉鏁�,鍑忔帀涓�涓紤浜у亣浜哄憳

print('yy:',name_dz_yy_num)

team_dz_xxm = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯缁�']

team_dz_xxm_OT = team_dz_xxm[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_dz_xxm_OT)

name_dz_xxm = team_dz_xxm[u'濮撳悕'].drop_duplicates()

name_dz_xxm_num = name_dz_xxm.count()聽 聽 #缁熻XX娴嬭瘯缁勪汉鏁�

print(name_dz_xxm_num)

team_dz_gj = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯缁�']

team_dz_gj_OT = team_dz_gj[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_dz_gj_OT)

name_dz_gj = team_dz_gj[u'濮撳悕'].drop_duplicates()

name_dz_gj_num = name_dz_gj.count()聽 聽 #缁熻XX娴嬭瘯缁勪汉鏁�

print(name_dz_gj_num)

#XX浼戜骇鍋�

team_dz_leader = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�']

team_dz_leader_OT= team_dz_leader[u'鍔犵彮鏃堕暱(鍒�)'].sum()聽 #鐢变簬浼戜骇鍋囷紝鍔犵彮鏃堕暱涓篘an

print('dz leader',team_dz_leader_OT)

name_dz_leader = team_dz_leader[u'濮撳悕'].drop_duplicates()

name_dz_leader_num = name_dz_leader.count() - 1聽 #浼戜骇鍋囷紝浜烘暟鍑�1

print(name_dz_leader_num)

team_dz_OT = sum([team_dz_cp01_OT,team_dz_cp02_OT,team_dz_xxm_OT,team_dz_gj_OT,

team_dz_yy_OT])聽 #XX娴嬭瘯閮ㄥ姞鐝椂闀跨粺璁�

print(team_dz_OT)

team_dz_num = sum([name_dz_gj_num,name_dz_xxm_num,name_dz_yy_num,name_dz_cp02_num,

name_dz_cp01_num],name_dz_leader_num)聽 #XX娴嬭瘯閮ㄤ汉鏁扮粺璁�

print(team_dz_num)

team_qp_dfqp = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯缁�']

team_qp_dfqp_OT = team_qp_dfqp[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_qp_dfqp_OT)

name_qp_dfqp = team_qp_dfqp[u'濮撳悕'].drop_duplicates()

name_qp_dfqp_num = name_qp_dfqp.count()聽 聽 #缁熻XX娴嬭瘯缁勪汉鏁�

print(name_qp_dfqp_num)

team_qp_jg = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯缁�']

team_qp_jg_OT = team_qp_jg[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_qp_jg_OT)

name_qp_jg = team_qp_jg[u'濮撳悕'].drop_duplicates()

name_qp_jg_num = name_qp_jg.count()聽 聽 #缁熻XX娴嬭瘯缁勪汉鏁�

print(name_qp_jg_num)

team_qp_ljf = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯缁�']

team_qp_ljf_OT = team_qp_ljf[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_qp_ljf_OT)

name_qp_ljf = team_qp_ljf[u'濮撳悕'].drop_duplicates()

name_qp_ljf_num = name_qp_ljf.count()聽 聽 #缁熻XX娴嬭瘯缁勪汉鏁�

print(name_qp_ljf_num)

team_qp_jd = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯缁�']

team_qp_jd_OT = team_qp_jd[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_qp_jd_OT)

name_qp_jd = team_qp_jd[u'濮撳悕'].drop_duplicates()

name_qp_jd_num = name_qp_jd.count()聽 聽 #缁熻XX娴嬭瘯缁勪汉鏁�

print(name_qp_jd_num)

team_qp_gg = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�/XX娴嬭瘯缁�']

team_qp_gg_OT = team_qp_gg[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_qp_gg_OT)

name_qp_gg = team_qp_gg[u'濮撳悕'].drop_duplicates()

name_qp_gg_num = name_qp_gg.count()聽 聽 #缁熻XX娴嬭瘯缁勪汉鏁�

print(name_qp_gg_num)

team_qp_leader = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX娴嬭瘯閮�']

team_qp_leader_OT = team_qp_leader[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_qp_leader_OT)

name_qp_leader = team_qp_leader[u'濮撳悕'].drop_duplicates()

name_qp_leader_num = name_qp_leader.count()

print(name_qp_leader_num)

team_qp_OT = sum([team_qp_leader_OT,team_qp_dfqp_OT,team_qp_jg_OT,team_qp_gg_OT,

team_qp_ljf_OT,team_qp_jd_OT]) #XX娴嬭瘯閮ㄥ姞鐝椂闀挎�诲拰

print(team_qp_OT)

team_qp_num = sum([name_qp_jg_num,name_qp_gg_num,name_qp_jd_num,name_qp_ljf_num,

name_qp_leader_num,name_qp_dfqp_num])聽 #XX娴嬭瘯閮ㄤ汉鏁扮粺璁�

team_zdh = df[df[u'鎵�鍦ㄤ腑蹇�'] == u'XX浜嬩笟閮�/娴嬭瘯閮�/XX寮�鍙戠粍']

team_zdh_OT = team_zdh[u'鍔犵彮鏃堕暱(鍒�)'].sum()

print(team_zdh_OT)

name_zdh = team_zdh[u'濮撳悕'].drop_duplicates()

name_zdh_num = name_zdh.count()聽 #缁熻XX娴嬭瘯缁勪汉鏁�

print(name_zdh_num)

date = df[df[u'宸ヤ綔绫诲瀷'] == u'宸ヤ綔鏃�']聽 #绛涢�夊伐浣滄棩澶╂暟

date01 = date[u'鏃ユ湡'].drop_duplicates() #鍘绘帀宸ヤ綔鏃ュぉ鏁颁腑鐨勯噸澶嶆暟鎹�

date_num = date01.count()聽 聽 聽 聽 聽 聽 聽 聽 #宸ヤ綔绫诲瀷涓哄伐浣滄棩鐨勬暟鎹粺璁�

print(date_num)

#鍒ゆ柇褰撴湀鏄惁鏈夋硶瀹氳妭鍋囨棩

#re = df[u'鍔犵彮鏃堕暱(鍒�)'].isnull().count()聽 #缁熻澶囨敞鍒楄〃涓虹┖鍊肩殑鏁伴噺,isnull()鍑芥暟娌℃湁鍙戞尌浣滅敤锛坵hy?锛�

re = sum(df[u'澶囨敞'].isnull())聽 聽 聽 #缁熻澶囨敞鍒楄〃涓虹┖鍊肩殑鏁伴噺

print('re:',re)

re_list = df[u'濮撳悕'].count()聽 聽 聽 #缁熻浠绘剰鍒楃殑鎬昏鏁�

print('re_list:',re_list)

if re ==re_list:聽 聽 聽 聽 聽 聽 #鍒ゆ柇澶囨敞鍒楄〃鐨勭┖鍊兼暟閲忔槸鍚︾瓑浜庢�昏鏁帮紝鏄紝鍒欐病鏈夋硶瀹氳妭鍋囨棩

#re = df[u'澶囨敞'].isnull()聽 #缁熻澶囨敞鍒楄〃涓虹┖鍊肩殑鏁伴噺

#if True in re:聽 聽 聽 聽 聽 聽 #鍙兘鍒ゆ柇澶囨敞鍒楄〃涓寘鍚┖鍊硷紝璁$畻缁撴灉涓嶅噯纭�

date_fd_num = 0聽 聽 聽 聽 #褰撴湀娉曞畾鑺傚亣鏃ヤ负0

else:

date_fd = df[df[u'澶囨敞'] == u'娉曞畾鑺傚亣鏃�']聽 #绛涢�夋硶瀹氳妭鍋囨棩

date_fd01 = date_fd[u'鏃ユ湡'].drop_duplicates()

date_fd_num = date_fd01.count()聽 #宸ヤ綔绫诲瀷涓烘硶瀹氳妭鍋囨棩鐨勬暟鎹粺璁�

print(date_fd_num)

data_total = date_num - date_fd_num聽 #璁$畻褰撴湀鐨勫伐浣滄棩澶╂暟

print('workday:',data_total)

wb = load_workbook(r'E:\pythonstudy\excel\data\sample.xlsx')聽 #鍔犺浇excel琛�

print wb.get_sheet_names()聽 #鎵撳嵃鍚勮〃鐨勮〃鍚�

ws3 = wb.create_sheet("sheet2")聽 #鍒涘缓鏂拌〃

ws3["A1"] = '閮ㄩ棬'

ws3["B1"] = '鍔犵彮鎬诲拰'聽 聽 聽 聽 聽 #鍗曞厓鏍艰祴鍊�

ws3["C1"] = '浜烘暟'

ws3["D1"] = '宸ヤ綔鏃ュぉ鏁�'

ws3["E1"] = '骞冲潎鍔犵彮鏃堕暱(鍒�)'

ws3.column_dimensions['A'].width = 30聽 #璁剧疆鍒楀

ws3.column_dimensions['B'].width = 10

ws3.column_dimensions['C'].width = 10

ws3.column_dimensions['D'].width = 20

ws3.column_dimensions['E'].width = 20

ws3["A2"] = '娴嬭瘯閮�'

ws3["A3"] = 'XX娴嬭瘯閮�'

ws3["A4"] = 'XX娴嬭瘯閮╘XX娴嬭瘯涓�缁�'

ws3["A5"] = 'XX娴嬭瘯閮╘XX娴嬭瘯浜岀粍'

ws3["A6"] = 'XX娴嬭瘯閮╘XX娴嬭瘯缁�'

ws3["A7"] = 'XX娴嬭瘯閮╘XX娴嬭瘯缁�'

ws3["A8"] = 'XX娴嬭瘯閮╘XX娴嬭瘯缁�'

ws3["A9"] = 'XX娴嬭瘯閮�'

ws3["A10"] = 'XX娴嬭瘯閮╘XX妫嬬墝娴嬭瘯缁�'

ws3["A11"] = 'XX娴嬭瘯閮╘XX娴嬭瘯缁�'

ws3["A12"] = 'XX娴嬭瘯閮╘XX娴嬭瘯缁�'

ws3["A13"] = 'XX娴嬭瘯閮╘XX娴嬭瘯缁�'

ws3["A14"] = 'XX娴嬭瘯閮╘XX娴嬭瘯缁�'

ws3["A15"] = 'XX寮�鍙戠粍'

ws3['B2'] = total_OT

ws3['B3'] = team_dz_OT

ws3['B4'] = team_dz_cp01_OT

ws3['B5'] = team_dz_cp02_OT

ws3['B6'] = team_dz_yy_OT

ws3['B7'] = team_dz_xxm_OT

ws3['B8'] = team_dz_gj_OT

ws3['B9'] = team_qp_OT

ws3['B10'] = team_qp_dfqp_OT

ws3['B11'] = team_qp_jg_OT

ws3['B12'] = team_qp_ljf_OT

ws3['B13'] = team_qp_jd_OT

ws3['B14'] = team_qp_gg_OT

ws3['B15'] = team_zdh_OT

ws3['C2'] = df_name_total

ws3['C3'] = team_dz_num聽 聽 #鏈幓鎺変紤浜у亣浜烘暟

ws3['C4'] = name_dz_cp01_num

ws3['C5'] = name_dz_cp02_num

ws3['C6'] = name_dz_yy_num

ws3['C7'] = name_dz_xxm_num

ws3['C8'] = name_dz_gj_num

ws3['C9'] = team_qp_num

ws3['C10'] = name_qp_dfqp_num

ws3['C11'] = name_qp_jg_num

ws3['C12'] = name_qp_ljf_num

ws3['C13'] = name_qp_jd_num

ws3['C14'] = name_qp_gg_num

ws3['C15'] = name_zdh_num

ws3['D2'] = data_total聽 聽 聽 聽 聽 聽 聽 聽 #鍚屼竴鍒楄〃璧嬬浉鍚屽�硷紝闇�瑕佹壘瀵绘洿绠�鍗曠殑鏂规硶

ws3['D3'] = data_total

ws3['D4'] = data_total

ws3['D5'] = data_total

ws3['D6'] = data_total

ws3['D7'] = data_total

ws3['D8'] = data_total

ws3['D9'] = data_total

ws3['D10'] = data_total

ws3['D11'] = data_total

ws3['D12'] = data_total

ws3['D13'] = data_total

ws3['D14'] = data_total

ws3['D15'] = data_total

ws3['E2'] = ws3['B2'].value//ws3['C2'].value//ws3['D2'].value #璁$畻骞冲潎鍔犵彮鏃堕暱锛屼笖闄ゅ彇鏁存暟

ws3['E3'] = ws3['B3'].value//ws3['C3'].value//ws3['D3'].value

ws3['E4'] = ws3['B4'].value//ws3['C4'].value//ws3['D4'].value

ws3['E5'] = ws3['B5'].value//ws3['C5'].value//ws3['D5'].value

ws3['E6'] = ws3['B6'].value//ws3['C6'].value//ws3['D6'].value

ws3['E7'] = ws3['B7'].value//ws3['C7'].value//ws3['D7'].value

ws3['E8'] = ws3['B8'].value//ws3['C8'].value//ws3['D8'].value

ws3['E9'] = ws3['B9'].value//ws3['C9'].value//ws3['D9'].value

ws3['E10'] = ws3['B10'].value//ws3['C10'].value//ws3['D10'].value

ws3['E11'] = ws3['B11'].value//ws3['C11'].value//ws3['D11'].value

ws3['E12'] = ws3['B12'].value//ws3['C12'].value//ws3['D12'].value

ws3['E13'] = ws3['B13'].value//ws3['C13'].value//ws3['D13'].value

ws3['E14'] = ws3['B14'].value//ws3['C14'].value//ws3['D14'].value

ws3['E15'] = ws3['B15'].value//ws3['C15'].value//ws3['D15'].value

wb.save('E:\pythonstudy\excel\data\sample01.xlsx')

你可能感兴趣的:(python 浣跨敤pandas搴撳垎鏋愬姞鐝暟鎹�)