python电影评价分析.dat_python-small-examples
###Kaggle电影数据分析实战
本项⽬基于Kaggle电影影评数据集,通过这个系列,你将学到如何进⾏数据探索性分析(EDA),学会使⽤数据分析利器`pandas`,会⽤绘图包`pyecharts`,以及EDA时可能遇到的各种实际问题及⼀些处理技巧。
通过这个⼩项⽬,⼤家将会掌握`pandas`主要常⽤函数的使⽤技巧,`matplotlib`绘制直⽅图,和`pyecharts`使⽤逻辑,具体以下13个知识点:
1 `创建DataFrame`,转换长数据为宽数据;
2 `导⼊数据`;
3 `处理组合值`;
4 `索引列`;
5 `连接两个表`;
6 `按列筛选`;
7 按照字段`分组`; 8 按照字段`排序`; 9 分组后使⽤`聚合函数`;10 绘制频率`分布直⽅图`绘制;11 `最⼩抽样量`的计算⽅法; 12 `数据去重`; 13 `结果分析`
注意:这些知识点`不是散落的`,⽽是通过求出喜剧电影排⾏榜,`这⼀个⽬标主线`把它们串联起来。
本项⽬需要导⼊的包:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pyecharts.charts import Bar,Grid,Line
import pyecharts.options as opts
from pyecharts.globals import ThemeType
```
####1 创建DataFrame
pandas中⼀个dataFrame实例:
```python
Out[89]:
a val
0 apple1 1.0
1 apple
2 2.0
2 apple
3 3.0
3 apple
4 4.0
4 apple
5 5.0
```
我们的**⽬标**是变为如下结构:
```python
a apple1 apple2 apple3 apple4 apple5
0 1.0 2.0 3.0 4.0 5.0
```
乍看可使⽤`pivot`,但很难⼀步到位。
所以另辟蹊径,提供⼀种简单且好理解的⽅法:
```python
In [113]: pd.DataFrame(index=[0],columns=df.a,data=dict(zip(df.a,df.val)))
Out[113]:
a apple1 apple2 apple3 apple4 apple5
0 1.0 2.0 3.0 4.0 5.0
```
以上⽅法是重新创建⼀个DataFrame,直接把`df.a`所有可能取值作为新dataframe的列,index调整为`[0]`,注意类型必须是数组类型(array-like 或者 Index),两个轴确定后,`data`填充数据域。
```python
In [116]: dict(zip(df.a,df.val))
Out[116]: {'apple1': 1.0, 'apple2': 2.0, 'apple3': 3.0, 'apple4': 4.0, 'apple5': 5.0}
```
####2 导⼊数据
数据来⾃kaggle,共包括三个⽂件:
1. movies.dat
2. ratings.dat
3. users.dat
`movies.dat`包括三个字段:['Movie ID', 'Movie Title', 'Genre']
使⽤pandas导⼊此⽂件:
```python
import pandas as pd
movies = pd.read_csv('./data/movietweetings/movies.dat', delimiter='::', engine='python', header=None, names = ['Movie ID', 'Movie Title', 'Genre'])
```
导⼊后,显⽰前5⾏:
```python
Movie ID Movie Title \
0 8 Edison Kinetoscopic Record of a Sneeze (1894)
1 10 La sortie des usines Lumi猫re (1895)
2 12 The Arrival of a Train (1896)
3 25 The Oxford and Cambridge University Boat Race ...
4 91 Le manoir du diable (1896)
5 131 Une nuit terrible (1896)
6 41
7 Le voyage dans la lune (1902)
7 439 The Great Train Robbery (1903)
8 443 Hiawatha, the Messiah of the Ojibway (1903)
9 628 The Adventures of Dollie (1908)
Genre
0 Documentary|Short
1 Documentary|Short
2 Documentary|Short
3 NaN
4 Short|Horror
5 Short|Comedy|Horror
6 Short|Action|Adventure|Comedy|Fantasy|Sci-Fi
python怎么读取dat文件7 Short|Action|Crime|Western
8 NaN
9 Action|Short
```
次导⼊其他两个数据⽂件
`users.dat`:
```python
users = pd.read_csv('./data/movietweetings/users.dat', delimiter='::', engine='python', header=None, names = ['User ID', 'Twitter ID'])
print(users.head())
```
结果:
```python
User ID Twitter ID
0 1 397291295
1 2 40501255
2 3 417333257
3 4 138805259
4 5 2452094989
5 6 391774225
6 7 47317010
7 8 84541461
8 9 2445803544
9 10 995885060
```
`rating.data`:
```python
ratings = pd.read_csv('./data/movietweetings/ratings.dat', delimiter='::', engine='python', header=None, names = ['User ID', 'Movie ID', 'Rating', 'Rating Timestamp'])
print(ratings.head())
```
结果:
```python
User ID Movie ID Rating Rating Timestamp
0 1 111161 10 1373234211
1 1 117060 7 1373415231
2 1 120755 6 1373424360
3 1 317919 6 1373495763
4 1 454876 10 1373621125
5 1 790724 8 1374641320
6 1 88297
7
8 1372898763
7 1 1229238 9 1373506523
8 1 1288558 5 1373154354
9 1 1300854 8 1377165712
```
**read_csv 使⽤说明**
说明,本次导⼊`dat`⽂件使⽤`ad_csv`函数。
第⼀个位置参数`./data/movietweetings/ratings.dat` 表⽰⽂件的相对路径
第⼆个关键字参数:`delimiter='::'`,表⽰⽂件分隔符使⽤`::`
后⾯⼏个关键字参数分别代表使⽤的引擎,⽂件没有表头,所以`header`为`None;`
导⼊后dataframe的列名使⽤`names`关键字设置,这个参数⼤家可以记住,⽐较有⽤。
Kaggle电影数据集第⼀节,我们使⽤数据处理利器 `pandas`, 函数`read_csv` 导⼊给定的三个数据⽂件。
```python
import pandas as pd
movies = pd.read_csv('./data/movietweetings/movies.dat', delimiter='::', engine='python', header=None, names = ['Movie ID', 'Movie Title', 'Genre'])
users = pd.read_csv('./data/movietweetings/users.dat', delimiter='::', engine='python', header=None, names = ['User ID',
'Twitter ID'])
ratings = pd.read_csv('./data/movietweetings/ratings.dat', delimiter='::', engine='python', header=None, names = ['User ID', 'Movie ID', 'Rating', 'Rating Timestamp'])
```
⽤到的`read_csv`,某些重要的参数,如何使⽤在上⼀节也有所提到。下⾯开始数据探索分析(EDA)
> 出得分前10喜剧(comedy)
####3 处理组合值
表`movies`字段`Genre`表⽰电影的类型,可能有多个值,分隔符为`|`,取值也可能为`None`.
针对这类字段取值,可使⽤Pandas中Series提供的`str`做⼀步转化,**注意它是向量级的**,下⼀步,如Python原⽣的`str`类似,使⽤`contains`判断是否含有`comedy`字符串:
```python
mask = movies.ains('comedy',case=False,na=False)
```
注意使⽤的两个参数:`case`, `na`
case为 False,表⽰对⼤⼩写不敏感;
na Genre列某个单元格为`NaN`时,我们使⽤的充填值,此处填充为`False`
返回的`mask`是⼀维的`Series`,结构与 movies.Genre相同,取值为True 或 False.
观察结果:
```python
0 False
1 False
2 False
3 False
4 False
5 True
6 True
7 False
8 False
9 False
Name: Genre, dtype: bool
```
#### 4 访问某列
得到掩码mask后,pandas⾮常⽅便地能提取出⽬标记录:
```python

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。