Excel数据处理

import pandas as pd

Define file paths

original_file_path = ‘test.xlsx’
new_file_path = ‘new_file.xlsx’

Load the original Excel file

df = pd.read_excel(original_file_path, sheet_name=‘Sheet1’, header=3)

Required columns

required_columns = [‘DIDs (HEX)’, ‘Name’, ‘Byte’]

Check for missing columns

missing_columns = set(required_columns) - set(df.columns)
if missing_columns:
raise ValueError(f"Missing columns in the data: {missing_columns}")

Subset the dataframe to the required columns

df_subset = df[required_columns]
print(df_subset)

Save the subset to a new Excel file

df_subset.to_excel(new_file_path, index=False, sheet_name=‘mid’)

Load the newly created file and drop the first row

df1 = pd.read_excel(new_file_path, sheet_name=‘mid’)
df1 = df1.drop(index=0)
df1.to_excel(new_file_path, index=False, sheet_name=‘mid’)

Load the original file again with a different header to extract specific data

df2 = pd.read_excel(original_file_path, sheet_name=‘Sheet1’, header=4)

Select the required columns (22 and 2E)

selected_data = df2[[22, ‘2E’]]
print(selected_data)

Ensure the indices align before concatenation

df1.reset_index(drop=True, inplace=True)
selected_data.reset_index(drop=True, inplace=True)

Concatenate the dataframes along the columns

df_combined = pd.concat([df1, selected_data], axis=1)
df_combined.to_excel(new_file_path, index=False, sheet_name=‘mid’)

print(f"Data successfully combined and saved to {new_file_path}")

相关推荐

  1. Excel 数据处理记录

    2024-07-15 01:52:01       48 阅读
  2. Excel数据处理

    2024-07-15 01:52:01       20 阅读
  3. 物流Excel报表用python处理并进行数据分析

    2024-07-15 01:52:01       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 01:52:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 01:52:01       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 01:52:01       58 阅读
  4. Python语言-面向对象

    2024-07-15 01:52:01       69 阅读

热门阅读

  1. 虚拟专用网络(VPN)技术的研究与应用

    2024-07-15 01:52:01       20 阅读
  2. 0711,0712,0713 进程,进程之间的通信

    2024-07-15 01:52:01       16 阅读
  3. ZooKeeper实现分布式锁

    2024-07-15 01:52:01       17 阅读
  4. 了解微前端和qiankun

    2024-07-15 01:52:01       19 阅读
  5. Qt易错总结

    2024-07-15 01:52:01       23 阅读
  6. OpenAI 发布官方 .NET 库

    2024-07-15 01:52:01       19 阅读
  7. 技术探索之kotlin浅谈

    2024-07-15 01:52:01       19 阅读
  8. 企业网络安全工具整合与管控建议

    2024-07-15 01:52:01       21 阅读
  9. 构建图像金字塔遍历不同的大小

    2024-07-15 01:52:01       18 阅读
  10. 开源项目有哪些机遇与挑战?

    2024-07-15 01:52:01       21 阅读
  11. GIS就业相关问题快问快答

    2024-07-15 01:52:01       15 阅读
  12. 深入理解CSS中的 :: 和 :

    2024-07-15 01:52:01       23 阅读
  13. 牛客周赛 Round 51

    2024-07-15 01:52:01       20 阅读
  14. Git 2.45.2源码安装

    2024-07-15 01:52:01       17 阅读
  15. hnust 1794: 机器翻译

    2024-07-15 01:52:01       22 阅读
  16. 老杜Mysql 基础教程 笔记

    2024-07-15 01:52:01       21 阅读
  17. 【Linux】Ubuntu配置JDK环境、MySQL环境

    2024-07-15 01:52:01       20 阅读
  18. docker 镜像导入导出的方法

    2024-07-15 01:52:01       19 阅读