根据数据文件GDP,使用函数进行数据处理,绘制GDP排名前十的国家随时间变化的图表
时间: 2023-06-12 15:08:19 浏览: 68
首先,需要读取数据文件GDP并进行处理。可以使用Python的pandas库来读取和处理数据。
```python
import pandas as pd
# 读取数据文件GDP
df = pd.read_csv('GDP.csv')
# 提取GDP排名前十的国家
top10_countries = df.groupby('Country').sum().sort_values(by='GDP', ascending=False).head(10).index.tolist()
# 提取top10国家的GDP数据
df_top10 = df[df['Country'].isin(top10_countries)]
# 将年份作为索引
df_top10.set_index('Year', inplace=True)
# 提取GDP数据并转置
data = df_top10.pivot(columns='Country', values='GDP')
```
接下来,使用matplotlib库来绘制图表。
```python
import matplotlib.pyplot as plt
# 绘制GDP排名前十的国家随时间变化的图表
plt.figure(figsize=(10, 6))
plt.plot(data)
plt.title('Top 10 Countries GDP from 1960 to 2019')
plt.xlabel('Year')
plt.ylabel('GDP (trillion US dollars)')
plt.legend(data.columns)
plt.show()
```
运行以上代码,即可得到GDP排名前十的国家随时间变化的图表。