利用2000部电影的信息,获取电影所有上映国家分别保存 至列表all counties. 利用pandas . 模块创建df_ counties(DataFrame)记录了电影的国家或地区信息。其中此对象的index属性为电影的名字(films列表),属性 columns为所有国家(列表all_ counties) 以及得分,最后再加一列为对应每一部电影的得分。 电影名字:肖申克的救赎;活着。所有国家:美国 中国大陆 中国香港 德国。肖申克的救赎 1 0 0 0;活着 0 1 1 0。
时间: 2023-02-06 13:08:52 浏览: 68
假设我们已经有了电影名字列表 films 和所有国家列表 all_countries,并且已经获取了每部电影在不同国家的上映信息,我们可以使用 pandas 模块创建一个 DataFrame 对象 df_countries 来存储这些信息。
首先,我们需要导入 pandas 模块:
```
import pandas as pd
```
然后,我们可以使用 films 列表作为索引,all_countries 列表作为列标签,创建一个空的 DataFrame 对象 df_countries:
```
df_countries = pd.DataFrame(index=films, columns=all_countries)
```
接下来,我们可以使用 df_countries 的 loc 属性来设置每部电影在不同国家的上映信息:
```
df_countries.loc['肖申克的救赎'] = [1, 0, 0, 0]
df_countries.loc['活着'] = [0, 1, 1, 0]
```
最后,我们可以再增加一列来记录对应每一部电影的得分,使用 df_countries 的 insert 方法即可:
```
df_countries.insert(len(all_countries), '得分', [100, 90])
```
最终,df_countries 的结构如下所示:
| | 美国 | 中国大陆 | 中国香港 | 德国 | 得分 |
|---:|-----:|----------:|----------:|-----:|-----:|
|肖申克的救赎| 1 | 0 | 0 | 0 | 100 |
| 活着 | 0 | 1 | 1 | 0 | 90 |