281 lines
14 KiB
Plaintext
281 lines
14 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"metadata": {},
|
|
"cell_type": "markdown",
|
|
"source": "按特定符号拆分多行",
|
|
"id": "40837ad11e77f2be"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"id": "initial_id",
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"ExecuteTime": {
|
|
"end_time": "2025-06-30T02:50:28.264173Z",
|
|
"start_time": "2025-06-30T02:50:28.028888Z"
|
|
}
|
|
},
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"df = pd.read_excel(r\"C:\\Users\\Administrator.DESKTOP-7IC2USJ\\Desktop\\新建文件夹\\枫车套餐卡.xlsx\",sheet_name='枫车套餐卡')\n",
|
|
"\n",
|
|
"# 使用 explode 拆分列\n",
|
|
"df['卡内容'] = df['卡内容'].str.split(';') # 先按分号分割成列表\n",
|
|
"df_exploded = df.explode('卡内容') # 再用 explode 展开为多行\n",
|
|
"\n",
|
|
"# 重置索引(可选)\n",
|
|
"df_exploded = df_exploded.reset_index(drop=True)\n",
|
|
"\n",
|
|
"print(df_exploded)\n",
|
|
"df_exploded.to_excel(r\"C:\\Users\\Administrator.DESKTOP-7IC2USJ\\Desktop\\新建文件夹\\枫车套餐卡_拆分.xlsx\",index=False)"
|
|
],
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
" user_id consume_amount consume_times last_date \\\n",
|
|
"0 621387 1000.0 13 2025-06-25 13:49:43 \n",
|
|
"1 621387 1000.0 13 2025-06-25 13:49:43 \n",
|
|
"2 519726 2000.0 54 2025-06-22 18:47:31 \n",
|
|
"3 519726 2000.0 54 2025-06-22 18:47:31 \n",
|
|
"4 629996 1000.0 12 2025-06-20 15:35:07 \n",
|
|
".. ... ... ... ... \n",
|
|
"498 529014 3337.0 26 2022-01-07 12:01:01 \n",
|
|
"499 529014 3337.0 26 2022-01-07 12:01:01 \n",
|
|
"500 530332 1000.0 8 2022-01-03 15:59:13 \n",
|
|
"501 531674 298.0 7 2021-12-11 10:44:33 \n",
|
|
"502 531674 298.0 7 2021-12-11 10:44:33 \n",
|
|
"\n",
|
|
" day_from_last_date is_promoted 手机号 卡名称 车牌号 \\\n",
|
|
"0 1 0 18872280328 储值卡充值赠品 鄂AJ652L \n",
|
|
"1 1 0 18872280328 储值卡充值赠品 鄂AJ652L \n",
|
|
"2 4 0 18907171776 储值卡充值赠品 鄂A54BM6 \n",
|
|
"3 4 0 18907171776 储值卡充值赠品 鄂A54BM6 \n",
|
|
"4 6 0 15797176191 储值卡充值赠品 鄂W5609E \n",
|
|
".. ... ... ... ... ... \n",
|
|
"498 1266 0 13277309000 298超值洗车年卡 鄂A518M7 \n",
|
|
"499 1266 0 13277309000 298超值洗车年卡 鄂A518M7 \n",
|
|
"500 1270 0 17786147829 储值卡充值赠品 鄂A9GV66 \n",
|
|
"501 1293 0 18607195500 298超值洗车年卡 鄂A45500 \n",
|
|
"502 1293 0 18607195500 298超值洗车年卡 鄂A45500 \n",
|
|
"\n",
|
|
" car_no_default model_name_default vehicle_remark \\\n",
|
|
"0 鄂AJ652L 广汽丰田 凯美瑞 2021 2.0L CVT 汽油 锋尚版 三厢 NaN \n",
|
|
"1 鄂AJ652L 广汽丰田 凯美瑞 2021 2.0L CVT 汽油 锋尚版 三厢 NaN \n",
|
|
"2 NaN NaN NaN \n",
|
|
"3 NaN NaN NaN \n",
|
|
"4 鄂W5609E NaN NaN \n",
|
|
".. ... ... ... \n",
|
|
"498 鄂A518M7 一汽奥迪 一汽奥迪A4 2015 2.0T CVT 汽油 技术型 三厢 NaN \n",
|
|
"499 鄂A518M7 一汽奥迪 一汽奥迪A4 2015 2.0T CVT 汽油 技术型 三厢 NaN \n",
|
|
"500 鄂A9GV66 北京奔驰 北京奔驰GLC级 2021 2.0T 手自一体 汽油 豪华型 SUV NaN \n",
|
|
"501 鄂A45500 宝马 宝马X5 2013 3.0T 手自一体 汽油 领先型 SUV NaN \n",
|
|
"502 鄂A45500 宝马 宝马X5 2013 3.0T 手自一体 汽油 领先型 SUV NaN \n",
|
|
"\n",
|
|
" model_id_default drive_mileage_default wx_nick \\\n",
|
|
"0 4028d06d7767050f0177dc2befcb3cd9 NaN NaN \n",
|
|
"1 4028d06d7767050f0177dc2befcb3cd9 NaN NaN \n",
|
|
"2 NaN NaN NaN \n",
|
|
"3 NaN NaN NaN \n",
|
|
"4 NaN NaN NaN \n",
|
|
".. ... ... ... \n",
|
|
"498 4028b28846b8bb580146c6933ea50187 83760.0 NaN \n",
|
|
"499 4028b28846b8bb580146c6933ea50187 83760.0 NaN \n",
|
|
"500 4028d06d7767050f017819d58310120c NaN 养车用户0186 \n",
|
|
"501 4028b2883b87e9f6013b91d4211f0c08 NaN NaN \n",
|
|
"502 4028b2883b87e9f6013b91d4211f0c08 NaN NaN \n",
|
|
"\n",
|
|
" 卡内剩余金额 id deadline 卡内容 rest \n",
|
|
"0 380.0 113470 2025-04-15 18:27:32 精洗(1) 1 \n",
|
|
"1 380.0 113470 2025-04-15 18:27:32 漆面打蜡(1) 1 \n",
|
|
"2 320.0 114560 2025-06-16 15:39:43 漆面打蜡(0) 1 \n",
|
|
"3 320.0 114560 2025-06-16 15:39:43 精洗(1) 1 \n",
|
|
"4 910.0 115679 2025-09-01 20:15:08 精洗(1) 1 \n",
|
|
".. ... ... ... ... ... \n",
|
|
"498 0.0 85820 2022-09-15 12:08:24 精洗(0) 1 \n",
|
|
"499 0.0 85820 2022-09-15 12:08:24 普洗(1) 1 \n",
|
|
"500 850.0 85595 2024-06-04 19:33:10 精洗(1) 1 \n",
|
|
"501 0.0 85949 2022-09-19 15:35:42 精洗(0) 1 \n",
|
|
"502 0.0 85949 2022-09-19 15:35:42 普洗(15) 1 \n",
|
|
"\n",
|
|
"[503 rows x 20 columns]\n"
|
|
]
|
|
}
|
|
],
|
|
"execution_count": 2
|
|
},
|
|
{
|
|
"metadata": {},
|
|
"cell_type": "markdown",
|
|
"source": "GUI",
|
|
"id": "f00dde4a7bb72875"
|
|
},
|
|
{
|
|
"metadata": {
|
|
"ExecuteTime": {
|
|
"end_time": "2025-06-30T02:55:28.299115Z",
|
|
"start_time": "2025-06-30T02:55:28.240933Z"
|
|
}
|
|
},
|
|
"cell_type": "code",
|
|
"source": [
|
|
"import sys\n",
|
|
"import pandas as pd\n",
|
|
"from PyQt5.QtWidgets import (\n",
|
|
" QApplication, QWidget, QPushButton, QLabel, QLineEdit,\n",
|
|
" QFileDialog, QVBoxLayout, QHBoxLayout, QComboBox, QMessageBox\n",
|
|
")\n",
|
|
"\n",
|
|
"\n",
|
|
"class ExcelSplitterApp(QWidget):\n",
|
|
" def __init__(self):\n",
|
|
" super().__init__()\n",
|
|
" self.setWindowTitle(\"Excel 列拆分工具\")\n",
|
|
" self.setGeometry(100, 100, 600, 300)\n",
|
|
"\n",
|
|
" self.file_path = None\n",
|
|
" self.sheet_names = []\n",
|
|
" self.df = None\n",
|
|
"\n",
|
|
" self.init_ui()\n",
|
|
"\n",
|
|
" def init_ui(self):\n",
|
|
" layout = QVBoxLayout()\n",
|
|
"\n",
|
|
" # 文件选择部分\n",
|
|
" file_layout = QHBoxLayout()\n",
|
|
" self.file_label = QLabel(\"未选择文件\")\n",
|
|
" select_file_btn = QPushButton(\"选择文件\")\n",
|
|
" select_file_btn.clicked.connect(self.select_file)\n",
|
|
" file_layout.addWidget(QLabel(\"Excel 文件:\"))\n",
|
|
" file_layout.addWidget(self.file_label)\n",
|
|
" file_layout.addWidget(select_file_btn)\n",
|
|
" layout.addLayout(file_layout)\n",
|
|
"\n",
|
|
" # sheet选择\n",
|
|
" self.sheet_combo = QComboBox()\n",
|
|
" self.sheet_combo.currentTextChanged.connect(self.load_columns)\n",
|
|
" layout.addWidget(QLabel(\"选择工作表:\"))\n",
|
|
" layout.addWidget(self.sheet_combo)\n",
|
|
"\n",
|
|
" # 分隔符输入\n",
|
|
" self.sep_input = QLineEdit(\";\")\n",
|
|
" layout.addWidget(QLabel(\"请输入分隔符:\"))\n",
|
|
" layout.addWidget(self.sep_input)\n",
|
|
"\n",
|
|
" # 列选择\n",
|
|
" self.col_combo = QComboBox()\n",
|
|
" layout.addWidget(QLabel(\"选择需要拆分的列:\"))\n",
|
|
" layout.addWidget(self.col_combo)\n",
|
|
"\n",
|
|
" # 执行按钮\n",
|
|
" run_btn = QPushButton(\"执行拆分并保存\")\n",
|
|
" run_btn.clicked.connect(self.process_and_save)\n",
|
|
" layout.addWidget(run_btn)\n",
|
|
"\n",
|
|
" self.setLayout(layout)\n",
|
|
"\n",
|
|
" def select_file(self):\n",
|
|
" file_path, _ = QFileDialog.getOpenFileName(self, \"选择 Excel 文件\", \"\", \"Excel Files (*.xlsx *.xls)\")\n",
|
|
" if file_path:\n",
|
|
" self.file_path = file_path\n",
|
|
" self.file_label.setText(file_path.split(\"/\")[-1])\n",
|
|
"\n",
|
|
" try:\n",
|
|
" # 获取所有 sheet 名称\n",
|
|
" excel_file = pd.ExcelFile(file_path)\n",
|
|
" self.sheet_names = excel_file.sheet_names\n",
|
|
" self.sheet_combo.clear()\n",
|
|
" self.sheet_combo.addItems(self.sheet_names)\n",
|
|
" except Exception as e:\n",
|
|
" QMessageBox.critical(self, \"错误\", f\"无法读取文件: {e}\")\n",
|
|
"\n",
|
|
" def load_columns(self):\n",
|
|
" if not self.file_path:\n",
|
|
" return\n",
|
|
" sheet_name = self.sheet_combo.currentText()\n",
|
|
" try:\n",
|
|
" self.df = pd.read_excel(self.file_path, sheet_name=sheet_name)\n",
|
|
" cols = list(self.df.columns)\n",
|
|
" self.col_combo.clear()\n",
|
|
" self.col_combo.addItems(cols)\n",
|
|
" except Exception as e:\n",
|
|
" QMessageBox.critical(self, \"错误\", f\"无法读取该工作表: {e}\")\n",
|
|
"\n",
|
|
" def process_and_save(self):\n",
|
|
" if self.df is None:\n",
|
|
" QMessageBox.warning(self, \"警告\", \"请先选择文件和工作表!\")\n",
|
|
" return\n",
|
|
"\n",
|
|
" col_name = self.col_combo.currentText()\n",
|
|
" sep = self.sep_input.text()\n",
|
|
"\n",
|
|
" if col_name not in self.df.columns:\n",
|
|
" QMessageBox.warning(self, \"警告\", \"所选列不存在!\")\n",
|
|
" return\n",
|
|
"\n",
|
|
" try:\n",
|
|
" # 拆分列\n",
|
|
" df = self.df.copy()\n",
|
|
" df[col_name] = df[col_name].astype(str).str.split(sep)\n",
|
|
" exploded_df = df.explode(col_name).reset_index(drop=True)\n",
|
|
"\n",
|
|
" # 保存文件\n",
|
|
" save_path, _ = QFileDialog.getSaveFileName(self, \"保存文件\", \"\", \"Excel Files (*.xlsx)\")\n",
|
|
" if save_path:\n",
|
|
" exploded_df.to_excel(save_path, index=False)\n",
|
|
" QMessageBox.information(self, \"成功\", \"文件已保存!\")\n",
|
|
"\n",
|
|
" except Exception as e:\n",
|
|
" QMessageBox.critical(self, \"错误\", f\"处理失败: {e}\")\n",
|
|
"\n",
|
|
"\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" app = QApplication(sys.argv)\n",
|
|
" window = ExcelSplitterApp()\n",
|
|
" window.show()\n",
|
|
" sys.exit(app.exec_())"
|
|
],
|
|
"id": "1a73cbdee1adb72f",
|
|
"outputs": [
|
|
{
|
|
"ename": "ImportError",
|
|
"evalue": "DLL load failed while importing QtWidgets: 找不到指定的模块。",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
|
|
"\u001B[1;31mImportError\u001B[0m Traceback (most recent call last)",
|
|
"Cell \u001B[1;32mIn[4], line 3\u001B[0m\n\u001B[0;32m 1\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01msys\u001B[39;00m\n\u001B[0;32m 2\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mpandas\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m \u001B[38;5;21;01mpd\u001B[39;00m\n\u001B[1;32m----> 3\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mPyQt5\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mQtWidgets\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m 4\u001B[0m QApplication, QWidget, QPushButton, QLabel, QLineEdit,\n\u001B[0;32m 5\u001B[0m QFileDialog, QVBoxLayout, QHBoxLayout, QComboBox, QMessageBox\n\u001B[0;32m 6\u001B[0m )\n\u001B[0;32m 9\u001B[0m \u001B[38;5;28;01mclass\u001B[39;00m \u001B[38;5;21;01mExcelSplitterApp\u001B[39;00m(QWidget):\n\u001B[0;32m 10\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m__init__\u001B[39m(\u001B[38;5;28mself\u001B[39m):\n",
|
|
"\u001B[1;31mImportError\u001B[0m: DLL load failed while importing QtWidgets: 找不到指定的模块。"
|
|
]
|
|
}
|
|
],
|
|
"execution_count": 4
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|