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)得出的结果,第一列没有品种号,如何解决
时间: 2023-08-22 22:08:35 浏览: 142
可以将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)
```
这样得出的结果就会包含品种号了。
阅读全文