import pandas as pd import os Workpath = 'D:\PACK数据\规范化文件\功能测试数据' for filename in os.listdir(Workpath): path_excel = os.path.join(Workpath, filename) da = pd.read_excel(path_excel, sheet_name='详细') data = da.groupby('步骤序号').tail(1) # Handling CAN0_Cell columns max_number_cell = 0 for column_name in data.columns: if isinstance(column_name, str) and 'CAN0_Cell_' in column_name: num_str = column_name.split('_')[-1] num_cell = int(num_str) max_number_cell = max(max_number_cell, num_cell) if num_cell < 500: # Adding new columns and setting their data as None for i in range(max_number_cell + 1, 491): new_column_name = f'CAN0_Cell_{i:03d}' data[new_column_name] = None # Sorting columns containing "CAN0_Cell_" column_names_cell = [col for col in data.columns if col.startswith('CAN0_Cell_')] sorted_column_names_cell = sorted(column_names_cell, key=lambda x: int(x.split('_')[-1]) if x.split('_')[-1].isdigit() else 0) data = data[sorted_column_names_cell],这个代码·运行后data中不包含CAN0_Cell的列就没了,怎么实现
时间: 2024-04-15 19:27:13 浏览: 189
很抱歉,我之前给出的代码有误导性,并且对于您的问题没有给出正确的解决方案。以下是一个修改后的代码,用于在DataFrame中筛选出以"CAN0_Cell_"开头的列,并按照列名中的数字顺序对这些列进行排序:
```python
import pandas as pd
import os
Workpath = 'D:\PACK数据\规范化文件\功能测试数据'
for filename in os.listdir(Workpath):
path_excel = os.path.join(Workpath, filename)
da = pd.read_excel(path_excel, sheet_name='详细')
data = da.groupby('步骤序号').tail(1)
# Handling CAN0_Cell columns
column_names_cell = [col for col in data.columns if col.startswith('CAN0_Cell_')]
sorted_column_names_cell = sorted(column_names_cell,
key=lambda x: int(x.split('_')[-1]) if x.split('_')[-1].isdigit() else 0)
data = data[sorted_column_names_cell]
# Adding missing CAN0_Cell columns
max_number_cell = int(sorted_column_names_cell[-1].split('_')[-1]) if sorted_column_names_cell else 0
for i in range(max_number_cell + 1, 491):
new_column_name = f'CAN0_Cell_{i:03d}'
data[new_column_name] = None
# Sorting columns again to include newly added columns
sorted_column_names_cell = sorted(data.columns,
key=lambda x: int(x.split('_')[-1]) if x.split('_')[-1].isdigit() else 0)
data = data[sorted_column_names_cell]
# Output the modified DataFrame
print(data)
```
在这个修改后的代码中,我们首先筛选出以"CAN0_Cell_"开头的列,并按照列名中的数字顺序对这些列进行排序。然后,我们通过获取最大数字来确定需要添加的缺失列,并逐个添加到DataFrame中。接下来,我们再次按照列名中的数字顺序对DataFrame进行排序,以确保新添加的列也按照正确的顺序包含在其中。
请注意,这里的代码仅仅是输出了修改后的DataFrame,您可以根据实际需求进行相应的处理或保存操作。如果对这个代码有任何疑问或需要进一步的帮助,请随时提问。
阅读全文