|

楼主 |
发表于 2024-7-22 10:04:10
|
显示全部楼层
import pandas as pd
def process_excel(input_file, output_file):
# 读取Excel文件
df = pd.read_excel(input_file)
# 根据焊缝数量不为0的行提取指定列
df1 = df[df['焊缝数量'] != 0][['管线号', '焊缝种类', '工艺卡编号']]
df2 = df[df['焊缝数量2'] != 0][['管线号', '焊缝种类2', '工艺卡编号2']]
# 重命名焊缝种类2和工艺卡编号2的列名,以便后续合并
df2.rename(columns={'焊缝种类2': '焊缝种类', '工艺卡编号2': '工艺卡编号'}, inplace=True)
# 合并两个数据框
merged_df = pd.concat([df1, df2])
# 对相同的焊缝种类和工艺卡编号进行分组求和
result_df = merged_df.groupby(['管线号', '焊缝种类', '工艺卡编号']).sum().reset_index()
# 对结果按照管线号进行排序
result_df.sort_values(by='管线号', inplace=True)
# 将结果写入新的Excel文件
result_df.to_excel(output_file, index=False)
if __name__ == "__main__":
input_file = "原始数据.xlsx"
output_file = "提取结果.xlsx"
process_excel(input_file, output_file)
AI写的这个代码看起来比较对了,运行出错呢,帮忙看下什么问题呢 |
|