Python提取速率
需求:提取速率需要有M提取到M前数值并添加回原始数据如果是0只能为空白
Python实现
import pandas as pd
import os
def extract_broadband_speed(speed):
if pd.notnull(speed) and 'M' in str(speed):
return str(speed).split('M')[0] + 'M'
else:
return ''
current_directory = os.getcwd()
files = [file for file in os.listdir(current_directory) if file.endswith('.xls')]
for file in files:
file_path = os.path.join(current_directory, file)
df = pd.read_excel(file_path)
df['速率提取'] = df['速率'].apply(extract_broadband_speed)
df['编码-修改'] = df['编码'].astype(str).str.encode('utf-8').str.decode('utf-8')
output_file_path = os.path.join(current_directory, file.split('.')[0] + '_modified.xls')
df.to_excel(output_file_path, index=False)
print(f"文件 {file} 处理完成,处理后的数据已保存到 {output_file_path}")