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],怎么修改才能使列名中分别包含CAN0_Temp_和CAN0_Cell_的列按照各自连接的数字进行排序
时间: 2024-04-18 13:33:33 浏览: 183
python数据分析与可视化 import pandas as pd import numpy as np import m
要使列名中分别包含"CAN0_Temp_"和"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 and CAN0_Temp columns
column_names_cell = [col for col in data.columns if col.startswith('CAN0_Cell_')]
column_names_temp = [col for col in data.columns if col.startswith('CAN0_Temp_')]
sorted_column_names_cell = sorted(column_names_cell,
key=lambda x: int(x.split('_')[-1]) if x.split('_')[-1].isdigit() else 0)
sorted_column_names_temp = sorted(column_names_temp,
key=lambda x: int(x.split('_')[-1]) if x.split('_')[-1].isdigit() else 0)
data = data[sorted_column_names_temp + sorted_column_names_cell]
# Output the modified DataFrame
print(data)
```
在这个修改后的代码中,我们首先分别筛选出以"CAN0_Cell_"和"CAN0_Temp_"开头的列,并按照各自连接的数字顺序对这些列进行排序。然后,我们将排序后的"CAN0_Temp_"列和"CAN0_Cell_"列连接起来,以生成最终的排序后的列名列表。最后,我们根据新的列名顺序重新排列DataFrame。
请注意,这里的代码仅仅是输出了修改后的DataFrame,您可以根据实际需求进行相应的处理或保存操作。如果对这个代码有任何疑问或需要进一步的帮助,请随时提问。
阅读全文