1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
| import pandas as pd
import numpy as np
def process_df(df, time_range=pd.date_range('2021-12-01', '2024-11-20', freq='D')):
# 去掉前两行
df = df.iloc[2:]
# 将第三行设置为header
df.columns = df.iloc[0]
# 去掉第四行
df = df.iloc[2:]
# 将日期列设置为index
df['日期'] = pd.to_datetime(df['日期'])
df.set_index('日期', inplace=True)
# 如果第一行第一个值不是数字则设为空
if str(df.iloc[0, 0]).isdigit() == False:
df.iloc[0, 0] = np.nan
# 将0值改为空值
df = df.replace(0, np.nan)
# 向前填充再向后填充
df = df.fillna(method='ffill').fillna(method='bfill')
print(df.head())
return df
def process_excel(input_path, output_path, sheet_names=None):
# 读取 Excel 文件中的所有 sheet 名称
if sheet_names is None:
sheet_names = pd.ExcelFile(input_path).sheet_names
# 处理数据
processed_df_dict = {}
for sheet_name in sheet_names:
print('正在处理', sheet_name)
df = pd.read_excel(input_path, header=None, sheet_name=sheet_name)
# 移除重复的索引
df = df.loc[~df.index.duplicated(keep='first')]
# 处理数据
processed_df = process_df(df)
processed_df_dict[sheet_name] = processed_df
# 获取公共索引
common_index = processed_df_dict[sheet_names[0]].index
for key in processed_df_dict:
processed_df_dict[key] = processed_df_dict[key].reindex(common_index)
# 输出每个 DataFrame 的 shape
for key in processed_df_dict:
print(f"{key} shape: {processed_df_dict[key].shape}")
# 拼接所有 DataFrame
df_all = pd.concat(processed_df_dict.values(), axis=1)
print(df_all)
print(df_all.shape)
# 从 index 为 2021-04-03 开始
new_start_date = pd.to_datetime('2021-04-03')
if new_start_date in df_all.index:
new_start_date_index = df_all.index.get_loc(new_start_date)
df_all = df_all.iloc[new_start_date_index:]
else:
print(f"索引中不包含日期 {new_start_date}")
# 向前填充
df_all = df_all.ffill() # 老版本pandas:df_all = df_all.fillna(method='ffill')
# 向后填充
df_all = df_all.bfill() # 老版本pandas:df_all = df_all.fillna(method='bfill')
# 输出为 Excel
df_all.index.name = 'date' # 注意可以修改为df_all.index.name = '日期',这样输出的excel文件中的日期列为日期
df_all.to_excel(output_path)
# 示例调用
input_path = 'Wind全部指标.xlsx'
output_path = 'test_all.xlsx'
process_excel(input_path, output_path)
output_path = 'test_part.xlsx'
sheet_names = ['Wind经济指数', 'Wind股票指数']
process_excel(input_path, output_path, sheet_names)
# 注意输出的excel文件中的日期列为date,如果需要修改可以修改df_all.index.name = 'date'这一行代码
# 如果想懒得确定,可以在之后使用pd.read_excel('test_all.xlsx',index_col=0)来读取数据,将第一列作为索引列
|