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

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值