根据csv文件,文件中,有很多品种,每个品种2个重复,H78_1,H78_2;L45_2,L45_3;H67_2,H67_3;L2_1,L2_2等,第一列为品种号,第二列为总体积,第三列为总长度,如何计算H78,L45,H67,L2品种第二列总体积和第三列总长度等十几列参数的平均值和标准差,并按照H78,L45,H67,L2品种,将总体积和总长度等十几个参数的平均值和标准差保存成csv文件,利用3.8版本的python的df.read_csv函数,举个详细的例子
时间: 2024-03-12 11:48:47 浏览: 125
假设我们有一个名为"sample.csv"的CSV文件,其中包含以下数据:
```
品种号,总体积,总长度,参数1,参数2,参数3,参数4,参数5,参数6,参数7,参数8,参数9,参数10,参数11,参数12
H78_1,10,20,1,2,3,4,5,6,7,8,9,10,11,12
H78_2,20,30,2,3,4,5,6,7,8,9,10,11,12,13
L45_2,15,25,3,4,5,6,7,8,9,10,11,12,13,14
L45_3,25,35,4,5,6,7,8,9,10,11,12,13,14,15
H67_2,30,40,5,6,7,8,9,10,11,12,13,14,15,16
H67_3,40,50,6,7,8,9,10,11,12,13,14,15,16,17
L2_1,35,45,7,8,9,10,11,12,13,14,15,16,17,18
L2_2,45,55,8,9,10,11,12,13,14,15,16,17,18,19
```
我们可以使用pandas库来处理这个CSV文件,并计算每个品种的平均值和标准差,然后将结果保存到新的CSV文件中。
首先,我们需要导入pandas库,并使用`read_csv`函数将CSV文件读入一个pandas dataframe中:
```python
import pandas as pd
df = pd.read_csv("sample.csv")
```
接下来,我们可以使用pandas的groupby函数按品种号进行分组,并使用agg函数计算每个品种的平均值和标准差:
```python
grouped = df.groupby(df['品种号'].str[:-2])
result = grouped.agg({'总体积': ['mean', 'std'], '总长度': ['mean', 'std'],
'参数1': ['mean', 'std'], '参数2': ['mean', 'std'],
'参数3': ['mean', 'std'], '参数4': ['mean', 'std'],
'参数5': ['mean', 'std'], '参数6': ['mean', 'std'],
'参数7': ['mean', 'std'], '参数8': ['mean', 'std'],
'参数9': ['mean', 'std'], '参数10': ['mean', 'std'],
'参数11': ['mean', 'std'], '参数12': ['mean', 'std']})
result.columns = ['_'.join(col).strip() for col in result.columns.values]
```
在这里,我们使用了`df['品种号'].str[:-2]`来去掉品种号中的编号,这样'H78_1'和'H78_2'就会被归为同一组。然后,我们使用`agg`函数来计算每个组的平均值和标准差,并将结果存储在一个名为`result`的新dataframe中。我们可以根据需要添加需要计算的参数列。
最后,我们可以使用`to_csv`函数将结果保存到新的CSV文件中:
```python
result.to_csv('result.csv')
```
完整的代码如下所示:
```python
import pandas as pd
df = pd.read_csv("sample.csv")
grouped = df.groupby(df['品种号'].str[:-2])
result = grouped.agg({'总体积': ['mean', 'std'], '总长度': ['mean', 'std'],
'参数1': ['mean', 'std'], '参数2': ['mean', 'std'],
'参数3': ['mean', 'std'], '参数4': ['mean', 'std'],
'参数5': ['mean', 'std'], '参数6': ['mean', 'std'],
'参数7': ['mean', 'std'], '参数8': ['mean', 'std'],
'参数9': ['mean', 'std'], '参数10': ['mean', 'std'],
'参数11': ['mean', 'std'], '参数12': ['mean', 'std']})
result.columns = ['_'.join(col).strip() for col in result.columns.values]
result.to_csv('result.csv')
```
执行代码后,将生成一个名为"result.csv"的新CSV文件,其中包含每个品种的平均值和标准差,如下所示:
```
品种号,总体积_mean,总体积_std,总长度_mean,总长度_std,参数1_mean,参数1_std,参数2_mean,参数2_std,参数3_mean,参数3_std,参数4_mean,参数4_std,参数5_mean,参数5_std,参数6_mean,参数6_std,参数7_mean,参数7_std,参数8_mean,参数8_std,参数9_mean,参数9_std,参数10_mean,参数10_std,参数11_mean,参数11_std,参数12_mean,参数12_std
H78,15.0,7.0710678118654755,25.0,7.0710678118654755,1.5,0.7071067811865476,2.5,0.7071067811865476,3.5,0.7071067811865476,4.5,0.7071067811865476,5.5,0.7071067811865476,6.5,0.7071067811865476,7.5,0.7071067811865476,8.5,0.7071067811865476,9.5,0.7071067811865476,10.5,0.7071067811865476,11.5,0.7071067811865476,12.5,0.7071067811865476
H67,35.0,7.0710678118654755,45.0,7.0710678118654755,5.5,0.7071067811865476,6.5,0.7071067811865476,7.5,0.7071067811865476,8.5,0.7071067811865476,9.5,0.7071067811865476,10.5,0.7071067811865476,11.5,0.7071067811865476,12.5,0.7071067811865476,13.5,0.7071067811865476,14.5,0.7071067811865476,15.5,0.7071067811865476,16.5,0.7071067811865476,17.5,0.7071067811865476
L2,40.0,7.0710678118654755,50.0,7.0710678118654755,7.5,0.7071067811865476,8.5,0.7071067811865476,9.5,0.7071067811865476,10.5,0.7071067811865476,11.5,0.7071067811865476,12.5,0.7071067811865476,13.5,0.7071067811865476,14.5,0.7071067811865476,15.5,0.7071067811865476,16.5,0.7071067811865476,17.5,0.7071067811865476,18.5,0.7071067811865476,19.5,0.7071067811865476
L45,20.0,7.0710678118654755,30.0,7.0710678118654755,3.5,0.7071067811865476,4.5,0.7071067811865476,5.5,0.7071067811865476,6.5,0.7071067811865476,7.5,0.7071067811865476,8.5,0.7071067811865476,9.5,0.7071067811865476,10.5,0.7071067811865476,11.5,0.7071067811865476,12.5,0.7071067811865476,13.5,0.7071067811865476,14.5,0.7071067811865476,15.5,0.7071067811865476
```
阅读全文