批量删除excel数据列,修改excel数据列的排序

import pandas as pd
import os
import datetime
#增加当天的交易时间
today = datetime.date.today()
print(today)
folder_path = 'D:\\tmp_financial\DailyK\FullAExcel'
folder_path2 = 'D:\\tmp_financial\DailyK\standard'
for filename in os.listdir(folder_path):
    if filename.endswith('.xlsx'):  # 检查是否为 Excel 文件
        file_path = os.path.join(folder_path, filename)
        file_path2 = os.path.join(folder_path2, filename)
        df = pd.read_excel(file_path)
        #删除excel数据列
        df.drop('涨跌额', axis=1, inplace=True)
        df.drop('成交额(千元)', axis=1, inplace=True)
        df.drop('换手率(自由流通股)', axis=1, inplace=True)
        df.drop('市盈率(总市值/净利润, 亏损的PE为空)', axis=1, inplace=True)
        df.drop('市净率(总市值/净资产)', axis=1, inplace=True)
        df.drop('市销率', axis=1, inplace=True)
        df.drop('市销率(TTM)', axis=1, inplace=True)
        df.drop('股息率 (%)', axis=1, inplace=True)
        df.drop('股息率(TTM)(%)', axis=1, inplace=True)
        df.drop('总股本 (万股)', axis=1, inplace=True)
        df.drop('流通股本 (万股)', axis=1, inplace=True)
        df.drop('自由流通股本 (万)', axis=1, inplace=True)
        df.drop('总市值 (万元)', axis=1, inplace=True)
        df.drop('volume_rolling_mean', axis=1, inplace=True)
        df.drop('量比5日', axis=1, inplace=True)
        df.drop('量比1日', axis=1, inplace=True)
        #修改excel数据列的顺序
        col_names = df.columns.tolist()
        col_names = [col_names[0], col_names[1], col_names[2], col_names[3], col_names[4], col_names[5], col_names[6],
                     col_names[7], col_names[13], col_names[14], col_names[15], col_names[16], col_names[8],
                     col_names[10],col_names[9], col_names[11], col_names[12], col_names[17]]
        df = df.reindex(columns=col_names)
        print(filename)
        with pd.ExcelWriter(file_path2, mode='a', engine='openpyxl') as writer:
            df.to_excel(writer, index=False)







你可能感兴趣的:(python数据处理,数据库,人工智能,大数据)