使用jupyter写一段程序,将film.csv内的地区,总票房还有排名这三列数据以世界地图的形式呈现出来,进行可视化,其中总票房这一数据内有美元标识符和千分号
时间: 2023-06-14 13:04:12 浏览: 139
以下是使用Python和Pandas库进行数据处理和可视化的示例代码:
首先,我们需要导入必要的库:
```python
import pandas as pd
import plotly.express as px
```
然后,我们读取film.csv文件并选择需要的列:
```python
data = pd.read_csv('film.csv', usecols=['Area', 'Total box office', 'Rank'])
```
接下来,我们需要对Total box office列进行数据清洗,去除美元符号和千分号,并将其转换为浮点数:
```python
data['Total box office'] = data['Total box office'].str.replace('$', '').str.replace(',', '').str.replace('%', '').astype(float)
```
现在,我们可以使用plotly.express库创建世界地图,并将Total box office和Rank数据映射到颜色和大小上:
```python
fig = px.scatter_geo(data, locations="Area", color="Total box office",
hover_name="Area", size="Rank",
projection="natural earth")
fig.show()
```
最后,我们可以使用show()方法显示世界地图可视化结果:
```python
fig.show()
```
完整代码如下:
```python
import pandas as pd
import plotly.express as px
data = pd.read_csv('film.csv', usecols=['Area', 'Total box office', 'Rank'])
data['Total box office'] = data['Total box office'].str.replace('$', '').str.replace(',', '').str.replace('%', '').astype(float)
fig = px.scatter_geo(data, locations="Area", color="Total box office",
hover_name="Area", size="Rank",
projection="natural earth")
fig.show()
```
阅读全文