python处理csv数据,根据第五列大小排列,分别提取前20%平均大小、中间60%平均大小、后20%平均大小。
时间: 2024-03-12 17:44:44 浏览: 15
好的,下面是代码实现:
```python
import pandas as pd
# 读取csv文件
df = pd.read_csv('data.csv')
# 按照第五列排序
df = df.sort_values(by=df.columns[4])
# 计算总行数
total_rows = len(df)
# 分别计算前20%和后20%的行数
top_rows = int(total_rows * 0.2)
bottom_rows = int(total_rows * 0.2)
# 计算中间60%的行数
middle_rows = total_rows - top_rows - bottom_rows
# 分别提取前20%、中间60%、后20%的数据
top_df = df.iloc[:top_rows]
middle_df = df.iloc[top_rows:(top_rows+middle_rows)]
bottom_df = df.iloc[(total_rows-bottom_rows):]
# 计算每个部分的平均大小
top_mean = top_df.iloc[:, 4].mean()
middle_mean = middle_df.iloc[:, 4].mean()
bottom_mean = bottom_df.iloc[:, 4].mean()
# 打印结果
print("前20%平均大小:", top_mean)
print("中间60%平均大小:", middle_mean)
print("后20%平均大小:", bottom_mean)
```
需要注意的是,代码中的`data.csv`需要替换成实际的csv文件路径。另外,代码中假设第五列为大小列,如果实际情况不是,需要将`df.columns[4]`改为实际的列名或索引。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)