PANDAS--删除空值 和 python--os修改文件名
import numpy as np
import os
np.set_printoptions(threshold=np.inf)
#显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', None)
#设置value的显示长度为100,默认为50
pd.set_option('max_colwidth',1000)
#读取文件夹下所有文件
import os
fp ='C:\\Users\\YP715HX\\Desktop\\New folder\\VONB to EY (10 Mar)'
file_name = os.listdir(fp)
print(file_name)
for i in file_name:
i=i.replace('XLSX','xlsx')
#拼接路径
path = os.path.join(fp,i)
print(path)
data=pd.read_excel(path)
data = data.copy()
#去除空行
data.dropna(axis=0)
data.dropna(axis=1)
if 'Year Month (YYYYMM)' in data.columns:
data.rename(columns={'Year Month (YYYYMM)':'Report Date'},inplace=True)
elif 'Reporting Date' in data.columns:
data.rename(columns={'Reporting Date':'Report Date'},inplace=True)
print(data.columns)
print(i)
#保存文件新路径
path_new = 'C:\\Users\\YP715HX\\Desktop\\New folder\\result'
path_new_file = os.path.join(path_new,i)
data.to_excel(path_new_file)
#修改文件后缀
filename = os.path.splitext(i)[0] #文件名
filetype = os.path.splitext(i)[1] #文件扩展名
path_new_file_BIG = os.path.join(path_new,f'{filename}.XLSX')
print(path_new_file_BIG)
#文件重命名
os.renames(path_new_file,path_new_file_BIG)
`