Python提取速率

2023-12-13 10:29:53

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')
    # 将处理后的数据保存到新的 Excel 文件中
    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}")

文章来源:https://blog.csdn.net/qq_43001132/article/details/134950461
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。