使用Python實(shí)現(xiàn)分組數(shù)據(jù)并保存到單獨(dú)的文件中
Python分組數(shù)據(jù)并保存到單獨(dú)的文件中
步驟 1: 導(dǎo)入所需的庫(kù)
import os import pandas as pd
步驟 2: 讀取 Excel 數(shù)據(jù)
# 讀取 Excel 數(shù)據(jù)
df = pd.read_excel("C:\\Users\\liuchunlin2\\Desktop\\新建XLSX 工作表.xlsx")步驟 3: 根據(jù)指定字段分組數(shù)據(jù)
# 根據(jù)學(xué)校、班級(jí)、老師字段分組 grouped = df.groupby(['學(xué)校', '班級(jí)', '老師'])
步驟 4: 創(chuàng)建保存拆分?jǐn)?shù)據(jù)的文件夾
# 新建文件夾路徑 folder_path = "C:\\Users\\liuchunlin2\\Desktop\\拆分?jǐn)?shù)據(jù)" os.makedirs(folder_path, exist_ok=True) # 檢查文件夾是否存在,若不存在則創(chuàng)建
步驟 5: 遍歷分組數(shù)據(jù)并保存到不同的 Excel 文件中
# 遍歷分組,并將每個(gè)分組的數(shù)據(jù)保存到不同的 Excel 文件中
for name, group in grouped:
school, grade, teacher = name
filename = f"{school}_{grade}_{teacher}.xlsx"
file_path = os.path.join(folder_path, filename)
group.to_excel(file_path, index=False)創(chuàng)建一個(gè)簡(jiǎn)單的圖形用戶界面,用于選擇 Excel 文件并指定分組列,然后將數(shù)據(jù)按照分組保存到不同的 Excel 文件中

步驟 1: 導(dǎo)入所需的庫(kù)
import tkinter as tk # 導(dǎo)入 tkinter 模塊,用于創(chuàng)建圖形用戶界面 from tkinter import filedialog # 導(dǎo)入 filedialog 子模塊,用于打開文件對(duì)話框 import pandas as pd # 導(dǎo)入 pandas 庫(kù),用于數(shù)據(jù)處理 import os # 導(dǎo)入 os 模塊,用于文件和目錄操作
步驟 2: 定義函數(shù),用于打開文件對(duì)話框并選擇 Excel 文件路徑
def browse_file():
# 打開文件對(duì)話框,限定文件類型為 Excel 文件 (*.xlsx)
filepath = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")])
# 清空文件路徑輸入框,并將選定的文件路徑插入到輸入框中
file_entry.delete(0, tk.END)
file_entry.insert(0, filepath)步驟 3: 定義函數(shù),用于處理數(shù)據(jù)并將其按指定列分組保存為多個(gè) Excel 文件
def process_data():
# 獲取輸入文件路徑和需要分組的列名
input_file = file_entry.get()
group_columns = [column_entry.get() for column_entry in column_entries if column_entry.get()]
# 檢查輸入是否完整
if not input_file or not group_columns:
result_label.config(text="Please provide input file path and group columns.")
return
try:
# 讀取 Excel 文件為 DataFrame,并按指定列進(jìn)行分組
df = pd.read_excel(input_file)
grouped = df.groupby(group_columns)
# 創(chuàng)建用于存儲(chǔ)分組數(shù)據(jù)的文件夾
folder_name = "Splitted_Data"
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# 將每個(gè)分組的數(shù)據(jù)保存為單獨(dú)的 Excel 文件
for name, group in grouped:
filename = f"{folder_name}/{'_'.join(name)}.xlsx"
group.to_excel(filename, index=False)
result_label.config(text="Data processing completed successfully.")
except Exception as e:
result_label.config(text=f"Error occurred: {str(e)}")步驟 4: 創(chuàng)建 tkinter 窗口對(duì)象并設(shè)置標(biāo)題
root = tk.Tk()
root.title("Excel Data Grouping Tool") # 設(shè)置窗口標(biāo)題步驟 5: 創(chuàng)建標(biāo)簽和輸入框,用于顯示和輸入 Excel 文件路徑
file_label = tk.Label(root, text="Excel File Path:") file_label.grid(row=0, column=0, padx=5, pady=5, sticky="w") file_entry = tk.Entry(root, width=50) file_entry.grid(row=0, column=1, padx=5, pady=5, sticky="we") browse_button = tk.Button(root, text="Browse", command=browse_file) browse_button.grid(row=0, column=2, padx=5, pady=5)
步驟 6: 創(chuàng)建標(biāo)簽、輸入框和按鈕,用于指定分組列名
column_label = tk.Label(root, text="Group Columns:") column_label.grid(row=1, column=0, padx=5, pady=5, sticky="w") column_entry = tk.Entry(root, width=50) column_entry.grid(row=1, column=1, padx=5, pady=5, sticky="we") column_entries = [column_entry] add_column_button = tk.Button(root, text="Add Column", command=lambda: add_column_entry()) add_column_button.grid(row=1, column=2, padx=5, pady=5)
步驟 7: 創(chuàng)建函數(shù),用于添加新的分組列輸入框
def add_column_entry():
new_column_entry = tk.Entry(root, width=50)
new_column_entry.grid(row=len(column_entries) + 1, column=1, padx=5, pady=5, sticky="we")
column_entries.append(new_column_entry)步驟 8: 創(chuàng)建按鈕,用于處理數(shù)據(jù)
process_button = tk.Button(root, text="Process Data", command=process_data) process_button.grid(row=2, column=2, padx=5, pady=10, sticky="e") # 調(diào)整位置至右側(cè)
步驟 9: 創(chuàng)建標(biāo)簽,用于顯示處理結(jié)果信息
result_label = tk.Label(root, text="") result_label.grid(row=len(column_entries) + 3, column=0, columnspan=3, padx=5, pady=5)
步驟 10: 啟動(dòng)主事件循環(huán)
root.mainloop()
完整代碼
import tkinter as tk # 導(dǎo)入 tkinter 模塊,用于創(chuàng)建圖形用戶界面
from tkinter import filedialog # 導(dǎo)入 filedialog 子模塊,用于打開文件對(duì)話框
import pandas as pd # 導(dǎo)入 pandas 庫(kù),用于數(shù)據(jù)處理
import os # 導(dǎo)入 os 模塊,用于文件和目錄操作
# 定義函數(shù),用于打開文件對(duì)話框并選擇 Excel 文件路徑
def browse_file():
# 打開文件對(duì)話框,限定文件類型為 Excel 文件 (*.xlsx)
filepath = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")])
# 清空文件路徑輸入框,并將選定的文件路徑插入到輸入框中
file_entry.delete(0, tk.END)
file_entry.insert(0, filepath)
# 定義函數(shù),用于處理數(shù)據(jù)并將其按指定列分組保存為多個(gè) Excel 文件
def process_data():
# 獲取輸入文件路徑和需要分組的列名
input_file = file_entry.get()
group_columns = [column_entry.get() for column_entry in column_entries if column_entry.get()]
# 檢查輸入是否完整
if not input_file or not group_columns:
result_label.config(text="Please provide input file path and group columns.")
return
try:
# 讀取 Excel 文件為 DataFrame,并按指定列進(jìn)行分組
df = pd.read_excel(input_file)
grouped = df.groupby(group_columns)
# 創(chuàng)建用于存儲(chǔ)分組數(shù)據(jù)的文件夾
folder_name = "Splitted_Data"
if not os.path.exists(folder_name):
os.makedirs(folder_name)
# 將每個(gè)分組的數(shù)據(jù)保存為單獨(dú)的 Excel 文件
for name, group in grouped:
filename = f"{folder_name}/{'_'.join(name)}.xlsx"
group.to_excel(filename, index=False)
result_label.config(text="Data processing completed successfully.")
except Exception as e:
result_label.config(text=f"Error occurred: {str(e)}")
# 創(chuàng)建 tkinter 窗口對(duì)象
root = tk.Tk()
root.title("Excel Data Grouping Tool") # 設(shè)置窗口標(biāo)題
# 創(chuàng)建標(biāo)簽和輸入框,用于顯示和輸入 Excel 文件路徑
file_label = tk.Label(root, text="Excel File Path:")
file_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
file_entry = tk.Entry(root, width=50)
file_entry.grid(row=0, column=1, padx=5, pady=5, sticky="we")
browse_button = tk.Button(root, text="Browse", command=browse_file)
browse_button.grid(row=0, column=2, padx=5, pady=5)
# 創(chuàng)建標(biāo)簽、輸入框和按鈕,用于指定分組列名
column_label = tk.Label(root, text="Group Columns:")
column_label.grid(row=1, column=0, padx=5, pady=5, sticky="w")
column_entry = tk.Entry(root, width=50)
column_entry.grid(row=1, column=1, padx=5, pady=5, sticky="we")
column_entries = [column_entry]
add_column_button = tk.Button(root, text="Add Column", command=lambda: add_column_entry())
add_column_button.grid(row=1, column=2, padx=5, pady=5)
# 創(chuàng)建函數(shù),用于添加新的分組列輸入框
def add_column_entry():
new_column_entry = tk.Entry(root, width=50)
new_column_entry.grid(row=len(column_entries) + 1, column=1, padx=5, pady=5, sticky="we")
column_entries.append(new_column_entry)
# 創(chuàng)建按鈕,用于處理數(shù)據(jù)
process_button = tk.Button(root, text="Process Data", command=process_data)
process_button.grid(row=2, column=2, padx=5, pady=10, sticky="e") # 調(diào)整位置至右側(cè)
# 創(chuàng)建標(biāo)簽,用于顯示處理結(jié)果信息
result_label = tk.Label(root, text="")
result_label.grid(row=len(column_entries) + 3, column=0, columnspan=3, padx=5, pady=5)
# 啟動(dòng)主事件循環(huán)
root.mainloop()以上就是使用Python實(shí)現(xiàn)分組數(shù)據(jù)并保存到單獨(dú)的文件中 的詳細(xì)內(nèi)容,更多關(guān)于Python分組數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?Pandas讀取Excel日期數(shù)據(jù)的異常處理方法
Excel文件是傳統(tǒng)的數(shù)據(jù)格式,但面對(duì)海量數(shù)據(jù)時(shí),用編程的方法來處理數(shù)據(jù)更有優(yōu)勢(shì),下面這篇文章主要給大家介紹了關(guān)于Python?Pandas讀取Excel日期數(shù)據(jù)的異常處理方法,需要的朋友可以參考下2022-02-02
一行Python代碼實(shí)現(xiàn)為圖片上版權(quán)
不知道大家會(huì)不會(huì)遇到這樣的情況,自己辛辛苦苦整理的攻略,分享給自己的一些朋友,結(jié)果分享有人堂而皇之地拿著這份攻略圖片去引流,并聲稱是自己整理的,真是豈有此理!本文就來用Python實(shí)現(xiàn)為圖片上版權(quán),需要的可以參考一下2023-01-01
python kmeans聚類簡(jiǎn)單介紹和實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了python kmeans聚類簡(jiǎn)單介紹和實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
淺談pytorch池化maxpool2D注意事項(xiàng)
今天小編就為大家分享一篇淺談pytorch池化maxpool2D注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
conda將python低版本環(huán)境升級(jí)到高版本的完整步驟
這篇文章主要給大家介紹了關(guān)于conda將python低版本環(huán)境升級(jí)到高版本的完整步驟,包括激活環(huán)境、升級(jí)Python版本、驗(yàn)證升級(jí)、處理依賴問題和測(cè)試環(huán)境等步驟,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
Python并發(fā)編程多進(jìn)程,多線程及GIL全局解釋器鎖
這篇文章主要介紹了Python并發(fā)編程多進(jìn)程,多線程及GIL全局解釋器鎖,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
Python使用Chrome插件實(shí)現(xiàn)爬蟲過程圖解
這篇文章主要介紹了Python使用Chrome插件實(shí)現(xiàn)爬蟲,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

