import pandas as pd df=pd.read_csv('H:/analysis_results/root_global_results_HN.csv') group_data=df.groupby(df['folder'].str[:-2]) #计算品种的平均值和标准差 #folder=df['folder'].str.split('').str[0] #mean=group_data.mean() #std=group_data.std() #print('mean:',round(mean,3)) #print('std:',round(std,3)) result=group_data.agg({'volume':['mean','std'], 'convex_volume':['mean','std'], 'surface_area':['mean','std'], 'length':['mean','std'], 'max_width':['mean','std'], 'max_depth':['mean','std']} ) #df = result.reset_index(inplace=True) # 将索引列转换为普通列result.reset_index(inplace=True) #result.columns = [''.join(col).strip() for col in result.columns.values] result.columns = result.columns.map(lambda x: f'{x[0]}_{x[1]}') result.index.name='folder' result.reset_index(inplace=True) #result = result.sort_values(by='folder') #result = result.sort_index() result = result[['folder', 'volume_mean', 'volume_std', 'convex_volume_mean', 'convex_volume_std', 'surface_area_mean', 'surface_area_std', 'length_mean', 'length_std', 'max_width_mean', 'max_width_std', 'max_depth_mean', 'max_depth_std']] result_path='H:/analysis_results/mean_std_HN.csv' result.to_csv(result_path,index=False) print('结果已保存到{}',format(result_path))如何根据在最后生成的csv文件中按照第一列folder字母后的数字进行排序生成csv文件
时间: 2023-08-22 19:09:03 浏览: 80
可以使用pandas中的sort_values()函数进行排序。例如,按照folder字母后的数字进行降序排序,可以使用以下代码:
```
result = result.sort_values(by='folder', key=lambda x: x.str.extract('(\d+)', expand=False).astype(int), ascending=False)
```
这里的`key`参数指定了一个lambda函数,将folder列中的数字提取出来并转换为整数,然后按照这个整数进行排序。`ascending=False`表示按照降序排列。
相关问题
import pandas as pd df=pd.read_csv('H:/analysis_results/root_global_results_HN.csv') group_data=df.groupby(df['folder'].str[:-2]) #计算品种的平均值和标准差 #folder=df['folder'].str.split('_').str[0] #mean=group_data.mean() #std=group_data.std() #print('mean:',round(mean,3)) #print('std:',round(std,3)) result=group_data.agg({'volume':['mean','std'], 'convex_volume':['mean','std'], 'surface_area':['mean','std'], 'length':['mean','std'], 'max_width':['mean','std'], 'max_depth':['mean','std']} ) result.columns = ['_'.join(col).strip() for col in result.columns.values] #result = result.sort_index() result_path='H:/analysis_results/mean_std_HN.csv' result.to_csv(result_path,index=False)得出的结果,第一列没有品种号,如何解决
可以将groupby的列和结果列合并,即将df['folder']列和result合并,然后再将合并后的结果按照品种号排序。代码如下:
```
import pandas as pd
df=pd.read_csv('H:/analysis_results/root_global_results_HN.csv')
group_data=df.groupby(df['folder'].str[:-2])
result=group_data.agg({'volume':['mean','std'],
'convex_volume':['mean','std'],
'surface_area':['mean','std'],
'length':['mean','std'],
'max_width':['mean','std'],
'max_depth':['mean','std']} )
result.columns = ['_'.join(col).strip() for col in result.columns.values]
result['folder'] = result.index
result = result[['folder', 'volume_mean', 'volume_std', 'convex_volume_mean', 'convex_volume_std',
'surface_area_mean', 'surface_area_std', 'length_mean', 'length_std', 'max_width_mean',
'max_width_std', 'max_depth_mean', 'max_depth_std']]
result = result.sort_values(by='folder')
result_path='H:/analysis_results/mean_std_HN.csv'
result.to_csv(result_path,index=False)
```
这样得出的结果就会包含品种号了。
阅读全文