利用2000部电影的信息,获取电影所有上映国家分别保存 至列表all counties. 利用pandas . 模块创建df_ counties(DataFrame)记录了电影的国家或地区信息。其中此对象的index属性为电影的名字(films列表),属性 columns为所有国家(列表all_ counties) 以及得分,最后再加一列为对应每一部电影的得分。 电影名字:肖申克的救赎;活着。所有国家:美国 中国大陆 中国香港 德国。肖申克的救赎 1 0 0 0;活着 0 1 1 0。
时间: 2023-02-13 20:50:55 浏览: 57
首先,您需要准备两个列表:一个列表 films 存储 2000 部电影的名字,另一个列表 all_countries 存储所有出现过的国家或地区。接下来,您可以使用 pandas 库中的 DataFrame 函数创建一个空的 DataFrame 对象 df_countries,并将 films 列表设置为索引(index 属性),all_countries 列表设置为列(columns 属性)。最后,您可以使用 df_countries['得分'] 属性设置对应电影的得分。
例如:
```
import pandas as pd
films = ['肖申克的救赎', '活着']
all_countries = ['美国', '中国大陆', '中国香港', '德国']
df_countries = pd.DataFrame(index=films, columns=all_countries)
df_countries.loc['肖申克的救赎', '美国'] = 1
df_countries.loc['肖申克的救赎', '中国大陆'] = 0
df_countries.loc['肖申克的救赎', '中国香港'] = 0
df_countries.loc['肖申克的救赎', '德国'] = 0
df_countries.loc['肖申克的救赎', '得分'] = 10
df_countries.loc['活着', '美国'] = 0
df_countries.loc['活着', '中国大陆'] = 1
df_countries.loc['活着', '中国香港'] = 1
df_countries.loc['活着', '德国'] = 0
df_countries.loc['活着', '得分'] = 9
print(df_countries)
```
输出结果为:
```
美国 中国大陆 中国香港 德国 得分
肖申克的救赎
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![json](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)