优化代码
This commit is contained in:
@@ -8,42 +8,6 @@
|
||||
}
|
||||
},
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# 加载\"C:\\Users\\lanyuanxiaoyao\\Documents\\Tushare\\日线行情\"路径下的所有CSV文件,放在一个dataframe表里,然后根据ts_code分组,把分组按ts_code命名,以CSV的格式存在“C:\\Users\\lanyuanxiaoyao\\Documents\\Tushare\\日线行情\\group”路径下\n",
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"# 定义路径\n",
|
||||
"input_path = \"/Users/lanyuanxiaoyao/SynologyDrive/data/Tushare/日线行情\"\n",
|
||||
"output_path = \"/Users/lanyuanxiaoyao/Documents/日线行情\"\n",
|
||||
"\n",
|
||||
"# 确保输出路径存在\n",
|
||||
"os.makedirs(output_path, exist_ok=True)\n",
|
||||
"\n",
|
||||
"# 获取所有CSV文件\n",
|
||||
"csv_files = [f for f in os.listdir(input_path) if f.endswith('.csv')]\n",
|
||||
"\n",
|
||||
"# 初始化一个空的DataFrame\n",
|
||||
"df = pd.DataFrame()\n",
|
||||
"\n",
|
||||
"# 逐个加载CSV文件到DataFrame\n",
|
||||
"for file in csv_files:\n",
|
||||
" file_path = os.path.join(input_path, file)\n",
|
||||
" temp_df = pd.read_csv(file_path)\n",
|
||||
" df = pd.concat([df, temp_df], ignore_index=True)\n",
|
||||
"\n",
|
||||
"# 根据ts_code分组\n",
|
||||
"grouped = df.groupby('ts_code')\n",
|
||||
"\n",
|
||||
"# 将每个分组保存为CSV文件\n",
|
||||
"for ts_code, group in grouped:\n",
|
||||
" output_file_path = os.path.join(output_path, f\"{ts_code}.csv\")\n",
|
||||
" group.to_csv(output_file_path, index=False)\n",
|
||||
"\n",
|
||||
"print(\"文件已成功分组并保存为CSV格式。\")\n"
|
||||
],
|
||||
"id": "6a10e07f5f498bc6",
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
@@ -53,7 +17,9 @@
|
||||
]
|
||||
}
|
||||
],
|
||||
"execution_count": 1
|
||||
"execution_count": 1,
|
||||
"source": "import pandas as pd",
|
||||
"id": "6a10e07f5f498bc6"
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
|
||||
Reference in New Issue
Block a user