Python提取速率

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}")

相关推荐

  1. Python提取xml节点

    2023-12-13 16:56:02       32 阅读
  2. 图像特征提取 python

    2023-12-13 16:56:02       8 阅读
  3. Python实战:文本内容提取

    2023-12-13 16:56:02       13 阅读
  4. 如何使用Python提取文件名

    2023-12-13 16:56:02       12 阅读
  5. iOS 提高Xcode运行速度

    2023-12-13 16:56:02       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-13 16:56:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-13 16:56:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-13 16:56:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-13 16:56:02       18 阅读

热门阅读

  1. Spring框架中的8种设计模式

    2023-12-13 16:56:02       37 阅读
  2. 每日一练 | 华为认证真题练习Day29

    2023-12-13 16:56:02       40 阅读
  3. 选择排序

    2023-12-13 16:56:02       38 阅读
  4. 【Android】通知(未完待续)

    2023-12-13 16:56:02       43 阅读
  5. PHP中什么是命名空间(Namespace)?

    2023-12-13 16:56:02       41 阅读
  6. 多个变量存储同一个地址

    2023-12-13 16:56:02       38 阅读
  7. 只需六个简单步骤即可开发自定义应用程序

    2023-12-13 16:56:02       27 阅读
  8. ADC芯片用途在哪些行业?

    2023-12-13 16:56:02       30 阅读