怎么用pandas对txt中的第四列做统计,画出扇形图
时间: 2024-05-08 17:20:52 浏览: 40
假设你的txt文件中以空格分隔,第四列为数值列,可以按照以下步骤进行操作:
1. 读取txt文件为pandas DataFrame:
```python
import pandas as pd
df = pd.read_csv('file.txt', sep=' ', header=None)
```
2. 对第四列做统计:
```python
counts = df[3].value_counts()
```
3. 画出扇形图:
```python
import matplotlib.pyplot as plt
plt.pie(counts.values, labels=counts.index, autopct='%1.1f%%')
plt.axis('equal')
plt.show()
```
完整代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('file.txt', sep=' ', header=None)
counts = df[3].value_counts()
plt.pie(counts.values, labels=counts.index, autopct='%1.1f%%')
plt.axis('equal')
plt.show()
```
阅读全文