Pandas

113 篇文章 9 订阅
5 篇文章 1 订阅

日萌社

人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)


1 Pandas介绍

  

  • 2008年WesMcKinney开发出的库
  • 专门用于数据挖掘的开源python库
  • 以Numpy为基础,借力Numpy模块在计算方面性能高的优势
  • 基于matplotlib,能够简便的画图
  • 独特的数据结构

2 为什么使用Pandas

Numpy已经能够帮助我们处理数据,能够结合matplotlib解决部分数据展示等问题,那么pandas学习的目的在什么地方呢?

  • 增强图表可读性

    • 回忆我们在numpy当中创建学生成绩表样式:

    • 返回结果:

array([[92, 55, 78, 50, 50],
       [71, 76, 50, 48, 96],
       [45, 84, 78, 51, 68],
       [81, 91, 56, 54, 76],
       [86, 66, 77, 67, 95],
       [46, 86, 56, 61, 99],
       [46, 95, 44, 46, 56],
       [80, 50, 45, 65, 57],
       [41, 93, 90, 41, 97],
       [65, 83, 57, 57, 40]])

如果数据展示为这样,可读性就会更友好:

3 小结

  • pandas的优势【了解】
    • 增强图表可读性
    • 便捷的数据处理能力
    • 读取文件方便
    • 封装了Matplotlib、Numpy的画图和计算

5.2 Pandas数据结构

Pandas中一共有三种数据结构,分别为:Series、DataFrame和MultiIndex(老版本中叫Panel )。

其中Series是一维数据结构,DataFrame是二维的表格型数据结构,MultiIndex是三维的数据结构。

1.Series

Series是一个类似于一维数组的数据结构,它能够保存任何类型的数据,比如整数、字符串、浮点数等,主要由一组数据和与之相关的索引两部分构成。

1.1 Series的创建

# 导入pandas
import pandas as pd

pd.Series(data=None, index=None, dtype=None)
  • 参数:
    • data:传入的数据,可以是ndarray、list等
    • index:索引,必须是唯一的,且与数据的长度相等。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。
    • dtype:数据的类型

通过已有数据创建

  • 指定内容,默认索引
pd.Series(np.arange(10))
# 运行结果
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
dtype: int64
  • 指定索引
pd.Series([6.7,5.6,3,10,2], index=[1,2,3,4,5])
# 运行结果
1     6.7
2     5.6
3     3.0
4    10.0
5     2.0
dtype: float64
  • 通过字典数据创建
color_count = pd.Series({'red':100, 'blue':200, 'green': 500, 'yellow':1000})
color_count
# 运行结果
blue       200
green      500
red        100
yellow    1000
dtype: int64

1.2 Series的属性

为了更方便地操作Series对象中的索引和数据,Series中提供了两个属性index和values

  • index
color_count.index

# 结果
Index(['blue', 'green', 'red', 'yellow'], dtype='object')
  • values
color_count.values

# 结果
array([ 200,  500,  100, 1000])

也可以使用索引来获取数据:

color_count[2]

# 结果
100

2.DataFrame

DataFrame是一个类似于二维数组或表格(如excel)的对象,既有行索引,又有列索引

  • 行索引,表明不同行,横向索引,叫index,0轴,axis=0
  • 列索引,表名不同列,纵向索引,叫columns,1轴,axis=1

2.1 DataFrame的创建

# 导入pandas
import pandas as pd

pd.DataFrame(data=None, index=None, columns=None)
  • 参数:

    • index:行标签。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。
    • columns:列标签。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。
  • 通过已有数据创建

举例一:

pd.DataFrame(np.random.randn(2,3))

回忆咱们在前面直接使用np创建的数组显示方式,比较两者的区别。

举例二:创建学生成绩表

# 生成10名同学,5门功课的数据
score = np.random.randint(40, 100, (10, 5))

# 结果
array([[92, 55, 78, 50, 50],
       [71, 76, 50, 48, 96],
       [45, 84, 78, 51, 68],
       [81, 91, 56, 54, 76],
       [86, 66, 77, 67, 95],
       [46, 86, 56, 61, 99],
       [46, 95, 44, 46, 56],
       [80, 50, 45, 65, 57],
       [41, 93, 90, 41, 97],
       [65, 83, 57, 57, 40]])

但是这样的数据形式很难看到存储的是什么的样的数据,可读性比较差!!

问题:如何让数据更有意义的显示

# 使用Pandas中的数据结构
score_df = pd.DataFrame(score)

给分数数据增加行列索引,显示效果更佳

效果:

  • 增加行、列索引
# 构造行索引序列
subjects = ["语文", "数学", "英语", "政治", "体育"]

# 构造列索引序列
stu = ['同学' + str(i) for i in range(score_df.shape[0])]

# 添加行索引
data = pd.DataFrame(score, columns=subjects, index=stu)

2.2 DataFrame的属性

  • shape
data.shape

# 结果
(10, 5)
  • index

DataFrame的行索引列表

data.index

# 结果
Index(['同学0', '同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9'], dtype='object')
  • columns

DataFrame的列索引列表

data.columns

# 结果
Index(['语文', '数学', '英语', '政治', '体育'], dtype='object')
  • values

直接获取其中array的值

data.values

array([[92, 55, 78, 50, 50],
       [71, 76, 50, 48, 96],
       [45, 84, 78, 51, 68],
       [81, 91, 56, 54, 76],
       [86, 66, 77, 67, 95],
       [46, 86, 56, 61, 99],
       [46, 95, 44, 46, 56],
       [80, 50, 45, 65, 57],
       [41, 93, 90, 41, 97],
       [65, 83, 57, 57, 40]])
  • T

转置

data.T

结果

  • head(5):显示前5行内容

如果不补充参数,默认5行。填入参数N则显示前N行

data.head(5)

  • tail(5):显示后5行内容

如果不补充参数,默认5行。填入参数N则显示后N行

data.tail(5)

2.3 DatatFrame索引的设置

需求:

2.3.1 修改行列索引值

stu = ["学生_" + str(i) for i in range(score_df.shape[0])]

# 必须整体全部修改
data.index = stu

注意:以下修改方式是错误的

# 错误修改方式
data.index[3] = '学生_3'

2.3.2 重设索引

  • reset_index(drop=False)
    • 设置新的下标索引
    • drop:默认为False,不删除原来索引,如果为True,删除原来的索引值
# 重置索引,drop=False
data.reset_index()

# 重置索引,drop=True
data.reset_index(drop=True)

2.3.3 以某列值设置为新的索引

  • set_index(keys, drop=True)
    • keys : 列索引名成或者列索引名称的列表
    • drop : boolean, default True.当做新的索引,删除原来的列

设置新索引案例

1、创建

df = pd.DataFrame({'month': [1, 4, 7, 10],
                    'year': [2012, 2014, 2013, 2014],
                    'sale':[55, 40, 84, 31]})

   month  sale  year
0  1      55    2012
1  4      40    2014
2  7      84    2013
3  10     31    2014

2、以月份设置新的索引

df.set_index('month')
       sale  year
month
1      55    2012
4      40    2014
7      84    2013
10     31    2014

3、设置多个索引,以年和月份

df = df.set_index(['year', 'month'])
df
            sale
year  month
2012  1     55
2014  4     40
2013  7     84
2014  10    31

注:通过刚才的设置,这样DataFrame就变成了一个具有MultiIndex的DataFrame。

3.MultiIndex与Panel

3.1 MultiIndex

MultiIndex是三维的数据结构;

多级索引(也称层次化索引)是pandas的重要功能,可以在Series、DataFrame对象上拥有2个以及2个以上的索引。

3.1.1 multiIndex的特性

打印刚才的df的行索引结果

df.index

MultiIndex(levels=[[2012, 2013, 2014], [1, 4, 7, 10]],
           labels=[[0, 2, 1, 2], [0, 1, 2, 3]],
           names=['year', 'month'])

多级或分层索引对象。

  • index属性
    • names:levels的名称
    • levels:每个level的元组值
df.index.names
# FrozenList(['year', 'month'])

df.index.levels
# FrozenList([[1, 2], [1, 4, 7, 10]])

3.1.2 multiIndex的创建

arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))

# 结果
MultiIndex(levels=[[1, 2], ['blue', 'red']],
           codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
           names=['number', 'color'])

3.2 Panel

3.2.1 panel的创建

  • class pandas.Panel(data=Noneitems=Nonemajor_axis=Noneminor_axis=None)

    • 作用:存储3维数组的Panel结构

    • 参数:

      • data : ndarray或者dataframe

      • items : 索引或类似数组的对象,axis=0

      • major_axis : 索引或类似数组的对象,axis=1

      • minor_axis : 索引或类似数组的对象,axis=2

p = pd.Panel(data=np.arange(24).reshape(4,3,2),
                 items=list('ABCD'),
                 major_axis=pd.date_range('20130101', periods=3),
                 minor_axis=['first', 'second'])

# 结果
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 3 (major_axis) x 2 (minor_axis)
Items axis: A to D
Major_axis axis: 2013-01-01 00:00:00 to 2013-01-03 00:00:00
Minor_axis axis: first to second

3.2.2 查看panel数据

p[:,:,"first"]
p["B",:,:]

注:Pandas从版本0.20.0开始弃用:推荐的用于表示3D数据的方法是通过DataFrame上的MultiIndex方法

4 小结

  • pandas的优势【了解】
    • 增强图表可读性
    • 便捷的数据处理能力
    • 读取文件方便
    • 封装了Matplotlib、Numpy的画图和计算
  • series【知道】
    • 创建
      • pd.Series([], index=[])
      • pd.Series({})
    • 属性
      • 对象.index
      • 对象.values
  • DataFrame【掌握】
    • 创建
      • pd.DataFrame(data=None, index=None, columns=None)
    • 属性
      • shape -- 形状
      • index -- 行索引
      • columns -- 列索引
      • values -- 查看值
      • T -- 转置
      • head() -- 查看头部内容
      • tail() -- 查看尾部内容
    • DataFrame索引
      • 修改的时候,需要进行全局修改
      • 对象.reset_index()
      • 对象.set_index(keys)
  • MultiIndex与Panel【了解】
    • multiIndex:
      • 类似ndarray中的三维数组
      • 创建:
        • pd.MultiIndex.from_arrays()
      • 属性:
        • 对象.index
    • panel:
      • pd.Panel(data, items, major_axis, minor_axis)
      • panel数据要是想看到,则需要进行索引到dataframe或者series才可以

5.3 基本数据操作

为了更好的理解这些基本操作,我们将读取一个真实的股票数据。关于文件操作,后面在介绍,这里只先用一下API

# 读取文件
data = pd.read_csv("./data/stock_day.csv")

# 删除一些列,让数据更简单些,再去做后面的操作
data = data.drop(["ma5","ma10","ma20","v_ma5","v_ma10","v_ma20"], axis=1)

1 索引操作

Numpy当中我们已经讲过使用索引选取序列和切片选择,pandas也支持类似的操作,也可以直接使用列名、行名

称,甚至组合使用。

1.1 直接使用行列索引(先列后行)

获取'2018-02-27'这天的'close'的结果

# 直接使用行列索引名字的方式(先列后行)
data['open']['2018-02-27']
23.53

# 不支持的操作
# 错误
data['2018-02-27']['open']
# 错误
data[:1, :2]

1.2 结合loc或者iloc使用索引

获取从'2018-02-27':'2018-02-22','open'的结果

# 使用loc:只能指定行列索引的名字
data.loc['2018-02-27':'2018-02-22', 'open']

2018-02-27    23.53
2018-02-26    22.80
2018-02-23    22.88
Name: open, dtype: float64

# 使用iloc可以通过索引的下标去获取
# 获取前3天数据,前5列的结果
data.iloc[:3, :5]

            open    high    close    low
2018-02-27    23.53    25.88    24.16    23.53
2018-02-26    22.80    23.78    23.53    22.80
2018-02-23    22.88    23.37    22.82    22.71

1.3 使用ix组合索引

Warning:Starting in 0.20.0, the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers.

获取行第1天到第4天,['open', 'close', 'high', 'low']这个四个指标的结果

# 使用ix进行下表和名称组合做引
data.ix[0:4, ['open', 'close', 'high', 'low']]

# 推荐使用loc和iloc来获取的方式
data.loc[data.index[0:4], ['open', 'close', 'high', 'low']]
data.iloc[0:4, data.columns.get_indexer(['open', 'close', 'high', 'low'])]

            open    close    high    low
2018-02-27    23.53    24.16    25.88    23.53
2018-02-26    22.80    23.53    23.78    22.80
2018-02-23    22.88    22.82    23.37    22.71
2018-02-22    22.25    22.28    22.76    22.02

2 赋值操作

对DataFrame当中的close列进行重新赋值为1

# 直接修改原来的值
data['close'] = 1
# 或者
data.close = 1

3 排序

排序有两种形式,一种对于索引进行排序,一种对于内容进行排序

3.1 DataFrame排序

  • 使用df.sort_values(by=, ascending=)
    • 单个键或者多个键进行排序,
    • 参数:
      • by:指定排序参考的键
      • ascending:默认升序
        • ascending=False:降序
        • ascending=True:升序
# 按照开盘价大小进行排序 , 使用ascending指定按照大小排序
data.sort_values(by="open", ascending=True).head()

# 按照多个键进行排序
data.sort_values(by=['open', 'high'])

  • 使用df.sort_index给索引进行排序

这个股票的日期索引原来是从大到小,现在重新排序,从小到大

# 对索引进行排序
data.sort_index()

3.2 Series排序

  • 使用series.sort_values(ascending=True)进行排序

series排序时,只有一列,不需要参数

data['p_change'].sort_values(ascending=True).head()

2015-09-01   -10.03
2015-09-14   -10.02
2016-01-11   -10.02
2015-07-15   -10.02
2015-08-26   -10.01
Name: p_change, dtype: float64
  • 使用series.sort_index()进行排序

与df一致

# 对索引进行排序
data['p_change'].sort_index().head()

2015-03-02    2.62
2015-03-03    1.44
2015-03-04    1.57
2015-03-05    2.02
2015-03-06    8.51
Name: p_change, dtype: float64

4 总结

  • 1.索引【掌握】
    • 直接索引 -- 先列后行,是需要通过索引的字符串进行获取
    • loc -- 先行后列,是需要通过索引的字符串进行获取
    • iloc -- 先行后列,是通过下标进行索引
    • ix -- 先行后列, 可以用上面两种方法混合进行索引
  • 2.赋值【知道】
    • data[""] = **
    • data. =
  • 3.排序【知道】
    • dataframe
      • 对象.sort_values()
      • 对象.sort_index()
    • series
      • 对象.sort_values()
      • 对象.sort_index()

5.4 DataFrame运算

1 算术运算

  • add(other)

比如进行数学运算加上具体的一个数字

data['open'].add(1)

2018-02-27    24.53
2018-02-26    23.80
2018-02-23    23.88
2018-02-22    23.25
2018-02-14    22.49
  • sub(other)'

2 逻辑运算

2.1 逻辑运算符号

  • 例如筛选data["open"] > 23的日期数据
    • data["open"] > 23返回逻辑结果
data["open"] > 23

2018-02-27     True
2018-02-26    False
2018-02-23    False
2018-02-22    False
2018-02-14    False
# 逻辑判断的结果可以作为筛选的依据
data[data["open"] > 23].head()

  • 完成多个逻辑判断,
data[(data["open"] > 23) & (data["open"] < 24)].head()

2.2 逻辑运算函数

  • query(expr)
    • expr:查询字符串

通过query使得刚才的过程更加方便简单

data.query("open<24 & open>23").head()
  • isin(values)

例如判断'open'是否为23.53和23.85

# 可以指定值进行一个判断,从而进行筛选操作
data[data["open"].isin([23.53, 23.85])]

3 统计运算

3.1 describe

综合分析: 能够直接得出很多统计结果,countmeanstdminmax 等

# 计算平均值、标准差、最大值、最小值
data.describe()

3.2 统计函数

Numpy当中已经详细介绍,在这里我们演示min(最小值), max(最大值), mean(平均值), median(中位数), var(方差), std(标准差),mode(众数)结果:

countNumber of non-NA observations
sumSum of values
meanMean of values
medianArithmetic median of values
minMinimum
maxMaximum
modeMode
absAbsolute Value
prodProduct of values
stdBessel-corrected sample standard deviation
varUnbiased variance
idxmaxcompute the index labels with the maximum
idxmincompute the index labels with the minimum

对于单个函数去进行统计的时候,坐标轴还是按照默认列“columns” (axis=0, default),如果要对行“index” 需要指定(axis=1)

  • max()、min()
# 使用统计函数:0 代表列求结果, 1 代表行求统计结果
data.max(0)

open                   34.99
high                   36.35
close                  35.21
low                    34.01
volume             501915.41
price_change            3.03
p_change               10.03
turnover               12.56
my_price_change         3.41
dtype: float64
  • std()、var()
# 方差
data.var(0)

open               1.545255e+01
high               1.662665e+01
close              1.554572e+01
low                1.437902e+01
volume             5.458124e+09
price_change       8.072595e-01
p_change           1.664394e+01
turnover           4.323800e+00
my_price_change    6.409037e-01
dtype: float64

# 标准差
data.std(0)

open                   3.930973
high                   4.077578
close                  3.942806
low                    3.791968
volume             73879.119354
price_change           0.898476
p_change               4.079698
turnover               2.079375
my_price_change        0.800565
dtype: float64
  • median():中位数

中位数为将数据从小到大排列,在最中间的那个数为中位数。如果没有中间数,取中间两个数的平均值。

df = pd.DataFrame({'COL1' : [2,3,4,5,4,2],
                   'COL2' : [0,1,2,3,4,2]})

df.median()

COL1    3.5
COL2    2.0
dtype: float64
  • idxmax()、idxmin()
# 求出最大值的位置
data.idxmax(axis=0)

open               2015-06-15
high               2015-06-10
close              2015-06-12
low                2015-06-12
volume             2017-10-26
price_change       2015-06-09
p_change           2015-08-28
turnover           2017-10-26
my_price_change    2015-07-10
dtype: object


# 求出最小值的位置
data.idxmin(axis=0)

open               2015-03-02
high               2015-03-02
close              2015-09-02
low                2015-03-02
volume             2016-07-06
price_change       2015-06-15
p_change           2015-09-01
turnover           2016-07-06
my_price_change    2015-06-15
dtype: object

3.3 累计统计函数

函数作用
cumsum计算前1/2/3/…/n个数的和
cummax计算前1/2/3/…/n个数的最大值
cummin计算前1/2/3/…/n个数的最小值
cumprod计算前1/2/3/…/n个数的积

那么这些累计统计函数怎么用?

以上这些函数可以对series和dataframe操作

这里我们按照时间的从前往后来进行累计

  • 排序
# 排序之后,进行累计求和
data = data.sort_index()
  • 对p_change进行求和
stock_rise = data['p_change']
# plot方法集成了前面直方图、条形图、饼图、折线图
stock_rise.cumsum()

2015-03-02      2.62
2015-03-03      4.06
2015-03-04      5.63
2015-03-05      7.65
2015-03-06     16.16
2015-03-09     16.37
2015-03-10     18.75
2015-03-11     16.36
2015-03-12     15.03
2015-03-13     17.58
2015-03-16     20.34
2015-03-17     22.42
2015-03-18     23.28
2015-03-19     23.74
2015-03-20     23.48
2015-03-23     23.74

那么如何让这个连续求和的结果更好的显示呢?

如果要使用plot函数,需要导入matplotlib.

import matplotlib.pyplot as plt
# plot显示图形
stock_rise.cumsum().plot()
# 需要调用show,才能显示出结果
plt.show()

关于plot,稍后会介绍API的选择

4 自定义运算

  • apply(func, axis=0)
    • func:自定义函数
    • axis=0:默认是列,axis=1为行进行运算
  • 定义一个对列,最大值-最小值的函数
data[['open', 'close']].apply(lambda x: x.max() - x.min(), axis=0)

open     22.74
close    22.85
dtype: float64

5 小结

  • 算术运算【知道】
  • 逻辑运算【知道】
    • 1.逻辑运算符号
    • 2.逻辑运算函数
      • 对象.query()
      • 对象.isin()
  • 统计运算【知道】
    • 1.对象.describe()
    • 2.统计函数
    • 3.累积统计函数
  • 自定义运算【知道】
    • apply(func, axis=0)

5.5 Pandas画图

1 pandas.DataFrame.plot

更多细节:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html?highlight=plot#pandas.DataFrame.plot

2 pandas.Series.plot

更多细节:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.plot.html?highlight=plot#pandas.Series.plot


5.6 文件读取与存储

我们的数据大部分存在于文件当中,所以pandas会支持复杂的IO操作,pandas的API支持众多的文件格式,如CSV、SQL、XLS、JSON、HDF5。

注:最常用的HDF5和CSV文件

1 CSV

1.1 read_csv

  • pandas.read_csv(filepath_or_buffer, sep =',', usecols )

    • filepath_or_buffer:文件路径
    • sep :分隔符,默认用","隔开
    • usecols:指定读取的列名,列表形式
  • 举例:读取之前的股票的数据

# 读取文件,并且指定只获取'open', 'close'指标
data = pd.read_csv("./data/stock_day.csv", usecols=['open', 'close'])

            open    close
2018-02-27    23.53    24.16
2018-02-26    22.80    23.53
2018-02-23    22.88    22.82
2018-02-22    22.25    22.28
2018-02-14    21.49    21.92

1.2 to_csv

  • DataFrame.to_csv(path_or_buf=None, sep=', ’, columns=None, header=True, index=True, mode='w', encoding=None)

    • path_or_buf :文件路径
    • sep :分隔符,默认用","隔开
    • columns :选择需要的列索引
    • header :boolean or list of string, default True,是否写进列索引值
    • index:是否写进行索引
    • mode:'w':重写, 'a' 追加
  • 举例:保存读取出来的股票数据

    • 保存'open'列的数据,然后读取查看结果
# 选取10行数据保存,便于观察数据
data[:10].to_csv("./data/test.csv", columns=['open'])
# 读取,查看结果
pd.read_csv("./data/test.csv")

     Unnamed: 0    open
0    2018-02-27    23.53
1    2018-02-26    22.80
2    2018-02-23    22.88
3    2018-02-22    22.25
4    2018-02-14    21.49
5    2018-02-13    21.40
6    2018-02-12    20.70
7    2018-02-09    21.20
8    2018-02-08    21.79
9    2018-02-07    22.69

会发现将索引存入到文件当中,变成单独的一列数据。如果需要删除,可以指定index参数,删除原来的文件,重新保存一次。

# index:存储不会讲索引值变成一列数据
data[:10].to_csv("./data/test.csv", columns=['open'], index=False)

2 HDF5

2.1 read_hdf与to_hdf

HDF5文件的读取和存储需要指定一个键,值为要存储的DataFrame

  • pandas.read_hdf(path_or_buf,key =None,** kwargs)

    从h5文件当中读取数据

    • path_or_buffer:文件路径
    • key:读取的键
    • return:Theselected object
  • DataFrame.to_hdf(path_or_buf, key*\kwargs*)

2.2 案例

  • 读取文件
day_close = pd.read_hdf("./data/day_close.h5")

如果读取的时候出现以下错误

需要安装安装tables模块避免不能读取HDF5文件

pip install tables

  • 存储文件
day_close.to_hdf("./data/test.h5", key="day_close")

再次读取的时候, 需要指定键的名字

new_close = pd.read_hdf("./data/test.h5", key="day_close")

注意:优先选择使用HDF5文件存储

  • HDF5在存储的时候支持压缩,使用的方式是blosc,这个是速度最快的也是pandas默认支持的
  • 使用压缩可以提磁盘利用率,节省空间
  • HDF5还是跨平台的,可以轻松迁移到hadoop 上面

3 JSON

JSON是我们常用的一种数据交换格式,前面在前后端的交互经常用到,也会在存储的时候选择这种格式。所以我们需要知道Pandas如何进行读取和存储JSON格式。

3.1 read_json

  • pandas.read_json(path_or_buf=None, orient=None, typ='frame', lines=False)

    • 将JSON格式准换成默认的Pandas DataFrame格式
    • orient : string,Indication of expected JSON string format.
      • 'split' : dict like {index -> [index], columns -> [columns], data -> [values]}
        • split 将索引总结到索引,列名到列名,数据到数据。将三部分都分开了
      • 'records' : list like [{column -> value}, ... , {column -> value}]
        • records 以columns:values的形式输出
      • 'index' : dict like {index -> {column -> value}}
        • index 以index:{columns:values}...的形式输出
      • 'columns' : dict like {column -> {index -> value}},默认该格式
        • colums 以columns:{index:values}的形式输出
      • 'values' : just the values array
        • values 直接输出值
    • lines : boolean, default False
      • 按照每行读取json对象
    • typ : default ‘frame’, 指定转换成的对象类型series或者dataframe

    3.2 read_josn 案例

  • 数据介绍

这里使用一个新闻标题讽刺数据集,格式为json。is_sarcastic:1讽刺的,否则为0;headline:新闻报道的标题;article_link:链接到原始新闻文章。存储格式为:

{"article_link": "https://www.huffingtonpost.com/entry/versace-black-code_us_5861fbefe4b0de3a08f600d5", "headline": "former versace store clerk sues over secret 'black code' for minority shoppers", "is_sarcastic": 0}
{"article_link": "https://www.huffingtonpost.com/entry/roseanne-revival-review_us_5ab3a497e4b054d118e04365", "headline": "the 'roseanne' revival catches up to our thorny political mood, for better and worse", "is_sarcastic": 0}
  • 读取

orient指定存储的json格式,lines指定按照行去变成一个样本

json_read = pd.read_json("./data/Sarcasm_Headlines_Dataset.json", orient="records", lines=True)

结果为:

3.3 to_json

  • DataFrame.to_json(path_or_buf=Noneorient=Nonelines=False)
    • 将Pandas 对象存储为json格式
    • path_or_buf=None:文件地址
    • orient:存储的json形式,{‘split’,’records’,’index’,’columns’,’values’}
    • lines:一个对象存储为一行

3.4 案例

  • 存储文件
json_read.to_json("./data/test.json", orient='records')

结果

[{"article_link":"https:\/\/www.huffingtonpost.com\/entry\/versace-black-code_us_5861fbefe4b0de3a08f600d5","headline":"former versace store clerk sues over secret 'black code' for minority shoppers","is_sarcastic":0},{"article_link":"https:\/\/www.huffingtonpost.com\/entry\/roseanne-revival-review_us_5ab3a497e4b054d118e04365","headline":"the 'roseanne' revival catches up to our thorny political mood, for better and worse","is_sarcastic":0},{"article_link":"https:\/\/local.theonion.com\/mom-starting-to-fear-son-s-web-series-closest-thing-she-1819576697","headline":"mom starting to fear son's web series closest thing she will have to grandchild","is_sarcastic":1},{"article_link":"https:\/\/politics.theonion.com\/boehner-just-wants-wife-to-listen-not-come-up-with-alt-1819574302","headline":"boehner just wants wife to listen, not come up with alternative debt-reduction ideas","is_sarcastic":1},{"article_link":"https:\/\/www.huffingtonpost.com\/entry\/jk-rowling-wishes-snape-happy-birthday_us_569117c4e4b0cad15e64fdcb","headline":"j.k. rowling wishes snape happy birthday in the most magical way","is_sarcastic":0},{"article_link":"https:\/\/www.huffingtonpost.com\/entry\/advancing-the-worlds-women_b_6810038.html","headline":"advancing the world's women","is_sarcastic":0},....]
  • 修改lines参数为True
json_read.to_json("./data/test.json", orient='records', lines=True)

结果

{"article_link":"https:\/\/www.huffingtonpost.com\/entry\/versace-black-code_us_5861fbefe4b0de3a08f600d5","headline":"former versace store clerk sues over secret 'black code' for minority shoppers","is_sarcastic":0}
{"article_link":"https:\/\/www.huffingtonpost.com\/entry\/roseanne-revival-review_us_5ab3a497e4b054d118e04365","headline":"the 'roseanne' revival catches up to our thorny political mood, for better and worse","is_sarcastic":0}
{"article_link":"https:\/\/local.theonion.com\/mom-starting-to-fear-son-s-web-series-closest-thing-she-1819576697","headline":"mom starting to fear son's web series closest thing she will have to grandchild","is_sarcastic":1}
{"article_link":"https:\/\/politics.theonion.com\/boehner-just-wants-wife-to-listen-not-come-up-with-alt-1819574302","headline":"boehner just wants wife to listen, not come up with alternative debt-reduction ideas","is_sarcastic":1}
{"article_link":"https:\/\/www.huffingtonpost.com\/entry\/jk-rowling-wishes-snape-happy-birthday_us_569117c4e4b0cad15e64fdcb","headline":"j.k. rowling wishes snape happy birthday in the most magical way","is_sarcastic":0}...

4 小结

  • pandas的CSV、HDF5、JSON文件的读取【知道】
    • 对象.read_**()
    • 对象.to_**()

5.7 高级处理-缺失值处理

1 如何处理nan

  • 获取缺失值的标记方式(NaN或者其他标记方式)

  • 如果缺失值的标记方式是NaN

    • 判断数据中是否包含NaN:

      • pd.isnull(df),
      • pd.notnull(df)
    • 存在缺失值nan:

      • 1、删除存在缺失值的:dropna(axis='rows')

        • 注:不会修改原数据,需要接受返回值
      • 2、替换缺失值:fillna(value, inplace=True)

        • value:替换成的值
        • inplace:True:会修改原数据,False:不替换修改原数据,生成新的对象
  • 如果缺失值没有使用NaN标记,比如使用"?"

    • 先替换‘?’为np.nan,然后继续处理

2 电影数据的缺失值处理

  • 电影数据文件获取
# 读取电影数据
movie = pd.read_csv("./data/IMDB-Movie-Data.csv")

2.1 判断缺失值是否存在

  • pd.notnull()
pd.notnull(movie)
Rank    Title    Genre    Description    Director    Actors    Year    Runtime (Minutes)    Rating    Votes    Revenue (Millions)    Metascore
0    True    True    True    True    True    True    True    True    True    True    True    True
1    True    True    True    True    True    True    True    True    True    True    True    True
2    True    True    True    True    True    True    True    True    True    True    True    True
3    True    True    True    True    True    True    True    True    True    True    True    True
4    True    True    True    True    True    True    True    True    True    True    True    True
5    True    True    True    True    True    True    True    True    True    True    True    True
6    True    True    True    True    True    True    True    True    True    True    True    True
7    True    True    True    True    True    True    True    True    True    True    False    True
np.all(pd.notnull(movie))
  • pd.isnull()

2.2 存在缺失值nan,并且是np.nan

  • 1、删除

pandas删除缺失值,使用dropna的前提是,缺失值的类型必须是np.nan

# 不修改原数据
movie.dropna()

# 可以定义新的变量接受或者用原来的变量名
data = movie.dropna()
  • 2、替换缺失值
# 替换存在缺失值的样本的两列
# 替换填充平均值,中位数
# movie['Revenue (Millions)'].fillna(movie['Revenue (Millions)'].mean(), inplace=True)

替换所有缺失值:

for i in movie.columns:
    if np.all(pd.notnull(movie[i])) == False:
        print(i)
        movie[i].fillna(movie[i].mean(), inplace=True)

2.3 不是缺失值nan,有默认标记的

数据是这样的:

wis = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data")

以上数据在读取时,可能会报如下错误:

URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)>

解决办法:

# 全局取消证书验证
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

处理思路分析:

  • 1、先替换‘?’为np.nan
    • df.replace(to_replace=, value=)
      • to_replace:替换前的值
      • value:替换后的值
# 把一些其它值标记的缺失值,替换成np.nan
wis = wis.replace(to_replace='?', value=np.nan)
  • 2、在进行缺失值的处理
# 删除
wis = wis.dropna()

3 小结

  • isnull、notnull判断是否存在缺失值【知道】
    • np.any(pd.isnull(movie)) # 里面如果有一个缺失值,就返回True
    • np.all(pd.notnull(movie)) # 里面如果有一个缺失值,就返回False
  • dropna删除np.nan标记的缺失值【知道】
    • movie.dropna()
  • fillna填充缺失值【知道】
    • movie[i].fillna(value=movie[i].mean(), inplace=True)
  • replace替换具体某些值【知道】
    • wis.replace(to_replace="?", value=np.NaN)

5.8 高级处理-数据离散化

1 为什么要离散化

连续属性离散化的目的是为了简化数据结构,数据离散化技术可以用来减少给定连续属性值的个数。离散化方法经常作为数据挖掘的工具。

2 什么是数据的离散化

连续属性的离散化就是在连续属性的值域上,将值域划分为若干个离散的区间,最后用不同的符号或整数 值代表落在每个子区间中的属性值。

离散化有很多种方法,这使用一种最简单的方式去操作

  • 原始人的身高数据:165,174,160,180,159,163,192,184
  • 假设按照身高分几个区间段:150~165, 165~180,180~195

这样我们将数据分到了三个区间段,我可以对应的标记为矮、中、高三个类别,最终要处理成一个"哑变量"矩阵

3 股票的涨跌幅离散化

我们对股票每日的"p_change"进行离散化

3.1 读取股票的数据

先读取股票的数据,筛选出p_change数据

data = pd.read_csv("./data/stock_day.csv")
p_change= data['p_change']

3.2 将股票涨跌幅数据进行分组

使用的工具:

  • pd.qcut(data, q):
    • 对数据进行分组将数据分组,一般会与value_counts搭配使用,统计每组的个数
  • series.value_counts():统计分组次数
# 自行分组
qcut = pd.qcut(p_change, 10)
# 计算分到每个组数据个数
qcut.value_counts()

自定义区间分组:

  • pd.cut(data, bins)
# 自己指定分组区间
bins = [-100, -7, -5, -3, 0, 3, 5, 7, 100]
p_counts = pd.cut(p_change, bins)

3.3 股票涨跌幅分组数据变成one-hot编码

  • 什么是one-hot编码

把每个类别生成一个布尔列,这些列中只有一列可以为这个样本取值为1.其又被称为独热编码。

把下图中左边的表格转化为使用右边形式进行表示:

  • pandas.get_dummies(dataprefix=None)

    • data:array-like, Series, or DataFrame

    • prefix:分组名字

# 得出one-hot编码矩阵
dummies = pd.get_dummies(p_counts, prefix="rise")

4 小结

  • 数据离散化【知道】
    • 可以用来减少给定连续属性值的个数
    • 在连续属性的值域上,将值域划分为若干个离散的区间,最后用不同的符号或整数值代表落在每个子区间中的属性值。
  • qcut、cut实现数据分组【知道】
    • qcut:大致分为相同的几组
    • cut:自定义分组区间
  • get_dummies实现哑变量矩阵【知道】

5.9 高级处理-合并

如果你的数据由多张表组成,那么有时候需要将不同的内容合并在一起分析

1 pd.concat实现数据合并

  • pd.concat([data1, data2], axis=1)
    • 按照行或列进行合并,axis=0为列索引,axis=1为行索引

比如我们将刚才处理好的one-hot编码与原数据合并

# 按照行索引进行
pd.concat([data, dummies], axis=1)

2 pd.merge

  • pd.merge(left, right, how='inner', on=None)
    • 可以指定按照两组数据的共同键值对合并或者左右各自
    • left: DataFrame
    • right: 另一个DataFrame
    • on: 指定的共同键
    • how:按照什么方式连接
Merge methodSQL Join NameDescription
leftLEFT OUTER JOINUse keys from left frame only
rightRIGHT OUTER JOINUse keys from right frame only
outerFULL OUTER JOINUse union of keys from both frames
innerINNER JOINUse intersection of keys from both frames

2.1 pd.merge合并

left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
                        'key2': ['K0', 'K1', 'K0', 'K1'],
                        'A': ['A0', 'A1', 'A2', 'A3'],
                        'B': ['B0', 'B1', 'B2', 'B3']})

right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
                        'key2': ['K0', 'K0', 'K0', 'K0'],
                        'C': ['C0', 'C1', 'C2', 'C3'],
                        'D': ['D0', 'D1', 'D2', 'D3']})

# 默认内连接
result = pd.merge(left, right, on=['key1', 'key2'])

  • 左连接
result = pd.merge(left, right, how='left', on=['key1', 'key2'])

  • 右连接
result = pd.merge(left, right, how='right', on=['key1', 'key2'])

  • 外链接
result = pd.merge(left, right, how='outer', on=['key1', 'key2'])

3 总结

  • pd.concat([数据1, 数据2], axis=**)【知道】
  • pd.merge(left, right, how=, on=)【知道】
    • how -- 以何种方式连接
    • on -- 连接的键的依据是哪几个

5.10 高级处理-交叉表与透视表

1 交叉表与透视表什么作用

探究股票的涨跌与星期几有关?

以下图当中表示,week代表星期几,1,0代表这一天股票的涨跌幅是好还是坏,里面的数据代表比例

可以理解为所有时间为星期一等等的数据当中涨跌幅好坏的比例

  • 交叉表:交叉表用于计算一列数据对于另外一列数据的分组个数(用于统计分组频率的特殊透视表)
    • pd.crosstab(value1, value2)
  • 透视表:透视表是将原有的DataFrame的列分别作为行索引和列索引,然后对指定的列应用聚集函数
    • data.pivot_table()
    • DataFrame.pivot_table([], index=[])

2 案例分析

2.1 数据准备

  • 准备两列数据,星期数据以及涨跌幅是好是坏数据
  • 进行交叉表计算
# 寻找星期几跟股票张得的关系
# 1、先把对应的日期找到星期几
date = pd.to_datetime(data.index).weekday
data['week'] = date

# 2、假如把p_change按照大小去分个类0为界限
data['posi_neg'] = np.where(data['p_change'] > 0, 1, 0)

# 通过交叉表找寻两列数据的关系
count = pd.crosstab(data['week'], data['posi_neg'])

但是我们看到count只是每个星期日子的好坏天数,并没有得到比例,该怎么去做?

  • 对于每个星期一等的总天数求和,运用除法运算求出比例
# 算数运算,先求和
sum = count.sum(axis=1).astype(np.float32)

# 进行相除操作,得出比例
pro = count.div(sum, axis=0)

2.2 查看效果

使用plot画出这个比例,使用stacked的柱状图

pro.plot(kind='bar', stacked=True)
plt.show()

2.3 使用pivot_table(透视表)实现

使用透视表,刚才的过程更加简单

# 通过透视表,将整个过程变成更简单一些
data.pivot_table(['posi_neg'], index='week')

3 小结

  • 交叉表与透视表的作用【知道】
    • 交叉表:计算一列数据对于另外一列数据的分组个数
    • 透视表:指定某一列对另一列的关系

5.11 高级处理-分组与聚合

分组与聚合通常是分析数据的一种方式,通常与一些统计函数一起使用,查看数据的分组情况

想一想其实刚才的交叉表与透视表也有分组的功能,所以算是分组的一种形式,只不过他们主要是计算次数或者计算比例!!看其中的效果:

1 什么分组与聚合

2 分组API

  • DataFrame.groupby(key, as_index=False)
    • key:分组的列数据,可以多个
  • 案例:不同颜色的不同笔的价格数据
col =pd.DataFrame({'color': ['white','red','green','red','green'], 'object': ['pen','pencil','pencil','ashtray','pen'],'price1':[5.56,4.20,1.30,0.56,2.75],'price2':[4.75,4.12,1.60,0.75,3.15]})

color    object    price1    price2
0    white    pen    5.56    4.75
1    red    pencil    4.20    4.12
2    green    pencil    1.30    1.60
3    red    ashtray    0.56    0.75
4    green    pen    2.75    3.15
  • 进行分组,对颜色分组,price进行聚合
# 分组,求平均值
col.groupby(['color'])['price1'].mean()
col['price1'].groupby(col['color']).mean()

color
green    2.025
red      2.380
white    5.560
Name: price1, dtype: float64

# 分组,数据的结构不变
col.groupby(['color'], as_index=False)['price1'].mean()

color    price1
0    green    2.025
1    red    2.380
2    white    5.560

3 星巴克零售店铺数据

现在我们有一组关于全球星巴克店铺的统计数据,如果我想知道美国的星巴克数量和中国的哪个多,或者我想知道中国每个省份星巴克的数量的情况,那么应该怎么办?

数据来源:https://www.kaggle.com/starbucks/store-locations/data

3.1 数据获取

从文件中读取星巴克店铺数据

# 导入星巴克店的数据
starbucks = pd.read_csv("./data/starbucks/directory.csv")

3.2 进行分组聚合

# 按照国家分组,求出每个国家的星巴克零售店数量
count = starbucks.groupby(['Country']).count()

画图显示结果

count['Brand'].plot(kind='bar', figsize=(20, 8))
plt.show()

假设我们加入省市一起进行分组

# 设置多个索引,set_index()
starbucks.groupby(['Country', 'State/Province']).count()

仔细观察这个结构,与我们前面讲的哪个结构类似??

与前面的MultiIndex结构类似

4 小结

  • groupby进行数据的分组【知道】
    • pandas中,抛开聚合谈分组,无意义

案例

1 需求

现在我们有一组从2006年到2016年1000部最流行的电影数据

数据来源:https://www.kaggle.com/damianpanek/sunday-eda/data

  • 问题1:我们想知道这些电影数据中评分的平均分,导演的人数等信息,我们应该怎么获取?
  • 问题2:对于这一组电影数据,如果我们想rating,runtime的分布情况,应该如何呈现数据?
  • 问题3:对于这一组电影数据,如果我们希望统计电影分类(genre)的情况,应该如何处理数据?

2 实现

首先获取导入包,获取数据

%matplotlib inline
import pandas  as pd 
import numpy as np
from matplotlib import pyplot as plt
#文件的路径
path = "./data/IMDB-Movie-Data.csv"
#读取文件
df = pd.read_csv(path)

2.1 问题一:

我们想知道这些电影数据中评分的平均分,导演的人数等信息,我们应该怎么获取?

  • 得出评分的平均分

使用mean函数

df["Rating"].mean()
  • 得出导演人数信息

求出唯一值,然后进行形状获取

## 导演的人数
# df["Director"].unique().shape[0]
np.unique(df["Director"]).shape[0]

644

2.2 问题二:

对于这一组电影数据,如果我们想Rating,Runtime (Minutes)的分布情况,应该如何呈现数据?

  • 直接呈现,以直方图的形式

选择分数列数据,进行plot

df["Rating"].plot(kind='hist',figsize=(20,8))

  • Rating进行分布展示

进行绘制直方图

plt.figure(figsize=(20,8),dpi=80)
plt.hist(df["Rating"].values,bins=20)
plt.show()

修改刻度的间隔

# 求出最大最小值
max_ = df["Rating"].max()
min_ = df["Rating"].min()

# 生成刻度列表
t1 = np.linspace(min_,max_,num=21)

# [ 1.9    2.255  2.61   2.965  3.32   3.675  4.03   4.385  4.74   5.095  5.45   5.805  6.16   6.515  6.87   7.225  7.58   7.935  8.29   8.645  9.   ]

# 修改刻度
plt.xticks(t1)

# 添加网格
plt.grid()

  • Runtime (Minutes)进行分布展示

进行绘制直方图

plt.figure(figsize=(20,8),dpi=80)
plt.hist(df["Runtime (Minutes)"].values,bins=20)
plt.show()

修改间隔

# 求出最大最小值
max_ = df["Runtime (Minutes)"].max()
min_ = df["Runtime (Minutes)"].min()

# # 生成刻度列表
t1 = np.linspace(min_,max_,num=21)

# 修改刻度
plt.xticks(np.linspace(min_,max_,num=21))

# 添加网格
plt.grid()

2.3 问题三:

对于这一组电影数据,如果我们希望统计电影分类(genre)的情况,应该如何处理数据?

  • 思路分析
    • 思路
      • 1、创建一个全为0的dataframe,列索引置为电影的分类,temp_df
      • 2、遍历每一部电影,temp_df中把分类出现的列的值置为1
      • 3、求和
  • 1、创建一个全为0的dataframe,列索引置为电影的分类,temp_df
# 进行字符串分割
temp_list = [i.split(",") for i in df["Genre"]]
# 获取电影的分类
genre_list = np.unique([i for j in temp_list for i in j]) 

# 增加新的列
temp_df = pd.DataFrame(np.zeros([df.shape[0],genre_list.shape[0]]),columns=genre_list)
  • 2、遍历每一部电影,temp_df中把分类出现的列的值置为1
for i in range(1000):
    #temp_list[i] ['Action','Adventure','Animation']
    temp_df.ix[i,temp_list[i]]=1
print(temp_df.sum().sort_values())
  • 3、求和,绘图
temp_df.sum().sort_values(ascending=False).plot(kind="bar",figsize=(20,8),fontsize=20,colormap="cool")


Musical        5.0
Western        7.0
War           13.0
Music         16.0
Sport         18.0
History       29.0
Animation     49.0
Family        51.0
Biography     81.0
Fantasy      101.0
Mystery      106.0
Horror       119.0
Sci-Fi       120.0
Romance      141.0
Crime        150.0
Thriller     195.0
Adventure    259.0
Comedy       279.0
Action       303.0
Drama        513.0
dtype: float64


series
series的创建
import pandas as pd
import numpy as np
pd.Series(np.arange(9))
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
dtype: int64
pd.Series([1.2, 2.3, 4.5, 5.0], index=[1, 2, 3, 4])
1    1.2
2    2.3
3    4.5
4    5.0
dtype: float64
color_count = pd.Series({"red":10, "green":20, "blue":100})
color_count
blue     100
green     20
red       10
dtype: int64
series的属性
color_count.index
Index(['blue', 'green', 'red'], dtype='object')
color_count.values
array([100,  20,  10])
color_count[0]
100
color_count[1]
20
DataFrame
DataFrame创建
pd.DataFrame(np.random.randn(2,3))

score = np.random.randint(40, 100, (10, 5))
score
array([[98, 85, 84, 93, 83],
       [73, 68, 64, 47, 87],
       [91, 58, 86, 92, 46],
       [62, 86, 99, 75, 66],
       [49, 73, 46, 61, 81],
       [43, 96, 47, 65, 90],
       [98, 84, 60, 73, 54],
       [59, 93, 58, 83, 43],
       [62, 66, 51, 88, 89],
       [66, 60, 63, 57, 51]])
score_df = pd.DataFrame(score)
score_df


subjects = ["语文", "数学", "英语", "政治", "体育"]
​
stu = ["同学"+ str(i) for i in range(score_df.shape[0])]
​
data = pd.DataFrame(score, columns=subjects, index=stu)
stu
['同学0', '同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9']
data

DataFrame的属性
data.shape
(10, 5)
data.index
Index(['同学0', '同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9'], dtype='object')
data.columns
Index(['语文', '数学', '英语', '政治', '体育'], dtype='object')
data.values
array([[98, 85, 84, 93, 83],
       [73, 68, 64, 47, 87],
       [91, 58, 86, 92, 46],
       [62, 86, 99, 75, 66],
       [49, 73, 46, 61, 81],
       [43, 96, 47, 65, 90],
       [98, 84, 60, 73, 54],
       [59, 93, 58, 83, 43],
       [62, 66, 51, 88, 89],
       [66, 60, 63, 57, 51]])
data.T

DataFrame索引值的设置
stu = ["同学_"+ str(i) for i in range(score_df.shape[0])]
​
data.index = stu
# stu
data


 

# data.index[2] = "同学__"

data.reset_index()

data.reset_index(drop=True)

df = pd.DataFrame({'month': [1, 4, 7, 10],
                    'year': [2012, 2014, 2013, 2014],
                    'sale':[55, 40, 84, 31]})
df

df.set_index("year")

df = df.set_index(["year", "month"])
df

MultiIndex与Panel
MultiIndex
df

df.index
MultiIndex(levels=[[2012, 2013, 2014], [1, 4, 7, 10]],
           labels=[[0, 2, 1, 2], [0, 1, 2, 3]],
           names=['year', 'month'])
df.index.names
FrozenList(['year', 'month'])
df.index.levels
FrozenList([[2012, 2013, 2014], [1, 4, 7, 10]])
arrays = [[1, 1, 2, 2], ["r", "b", "r","b"]]
pd.MultiIndex.from_arrays(arrays, names=("num", "col"))
MultiIndex(levels=[[1, 2], ['b', 'r']],
           labels=[[0, 0, 1, 1], [1, 0, 1, 0]],
           names=['num', 'col'])
panel
p = pd.Panel(data=np.arange(24).reshape(4,3,2),
                 items=list('ABCD'),
                 major_axis=pd.date_range('20130101', periods=3),
                 minor_axis=['first', 'second'])
p
<class 'pandas.core.panel.Panel'>
Dimensions: 4 (items) x 3 (major_axis) x 2 (minor_axis)
Items axis: A to D
Major_axis axis: 2013-01-01 00:00:00 to 2013-01-03 00:00:00
Minor_axis axis: first to second


In [1]:

import pandas as pd

In [2]:

data = pd.read_csv("./data/stock_day.csv")

In [4]:

data.head()

Out[4]:

openhighcloselowvolumeprice_changep_changema5ma10ma20v_ma5v_ma10v_ma20turnover
2018-02-2723.5325.8824.1623.5395578.030.632.6822.94222.14222.87553782.6446738.6555576.112.39
2018-02-2622.8023.7823.5322.8060985.110.693.0222.40621.95522.94240827.5242736.3456007.501.53
2018-02-2322.8823.3722.8222.7152914.010.542.4221.93821.92923.02235119.5841871.9756372.851.32
2018-02-2222.2522.7622.2822.0236105.010.361.6421.44621.90923.13735397.5839904.7860149.600.90
2018-02-1421.4921.9921.9221.4823331.040.442.0521.36621.92323.25333590.2142935.7461716.110.58

In [5]:

data = data.drop(["ma5","ma10","ma20","v_ma5","v_ma10","v_ma20"], axis=1)

In [7]:

data.head()

Out[7]:

openhighcloselowvolumeprice_changep_changeturnover
2018-02-2723.5325.8824.1623.5395578.030.632.682.39
2018-02-2622.8023.7823.5322.8060985.110.693.021.53
2018-02-2322.8823.3722.8222.7152914.010.542.421.32
2018-02-2222.2522.7622.2822.0236105.010.361.640.90
2018-02-1421.4921.9921.9221.4823331.040.442.050.58

索引操作

In [8]:

data["open"]["2018-02-27"] # 直接索引,必须是先列后行

Out[8]:

23.53

In [11]:

# data["2018-02-27"]["open"]

In [13]:

# data[:1 ,:2]

In [14]:

data.loc["2018-02-27":"2018-02-14", "open":"close"]

Out[14]:

openhighclose
2018-02-2723.5325.8824.16
2018-02-2622.8023.7823.53
2018-02-2322.8823.3722.82
2018-02-2222.2522.7622.28
2018-02-1421.4921.9921.92

In [15]:

data.iloc[:5, :3]

Out[15]:

openhighclose
2018-02-2723.5325.8824.16
2018-02-2622.8023.7823.53
2018-02-2322.8823.3722.82
2018-02-2222.2522.7622.28
2018-02-1421.4921.9921.92

In [16]:

data.ix[0:5, ["open", "close"]]

Out[16]:

openclose
2018-02-2723.5324.16
2018-02-2622.8023.53
2018-02-2322.8822.82
2018-02-2222.2522.28
2018-02-1421.4921.92

In [17]:

data.loc[data.index[0:5], ["open", "close"]]

Out[17]:

openclose
2018-02-2723.5324.16
2018-02-2622.8023.53
2018-02-2322.8822.82
2018-02-2222.2522.28
2018-02-1421.4921.92

In [19]:

data.columns.get_indexer(["open", "close"])

Out[19]:

array([0, 2])

In [20]:

data.iloc[0:5, data.columns.get_indexer(["open", "close"])]

Out[20]:

openclose
2018-02-2723.5324.16
2018-02-2622.8023.53
2018-02-2322.8822.82
2018-02-2222.2522.28
2018-02-1421.4921.92

赋值操作

In [22]:

data["close"] = 1

In [24]:

data.head()

Out[24]:

openhighcloselowvolumeprice_changep_changeturnover
2018-02-2723.5325.88123.5395578.030.632.682.39
2018-02-2622.8023.78122.8060985.110.693.021.53
2018-02-2322.8823.37122.7152914.010.542.421.32
2018-02-2222.2522.76122.0236105.010.361.640.90
2018-02-1421.4921.99121.4823331.040.442.050.58

In [26]:

data.close = 10

In [27]:

data.head()

Out[27]:

openhighcloselowvolumeprice_changep_changeturnover
2018-02-2723.5325.881023.5395578.030.632.682.39
2018-02-2622.8023.781022.8060985.110.693.021.53
2018-02-2322.8823.371022.7152914.010.542.421.32
2018-02-2222.2522.761022.0236105.010.361.640.90
2018-02-1421.4921.991021.4823331.040.442.050.58

排序

In [30]:

data.sort_values(by="open", ascending=False).head()

Out[30]:

openhighcloselowvolumeprice_changep_changeturnover
2015-06-1534.9934.991031.69199369.53-3.52-10.006.82
2015-06-1234.6935.981034.01159825.880.822.385.47
2015-06-1034.1036.351032.23269033.120.511.539.21
2017-11-0133.8534.341033.10232325.30-0.61-1.775.81
2015-06-1133.1734.981032.51173075.730.541.595.92

In [32]:

data.sort_values(by=["open", "high"]).head()

Out[32]:

openhighcloselowvolumeprice_changep_changeturnover
2015-03-0212.2512.671012.2096291.730.322.623.30
2015-09-0212.3014.111012.3070201.74-1.10-8.172.40
2015-03-0312.5213.061012.52139071.610.181.444.76
2015-03-0412.8012.921012.6167075.440.201.572.30
2015-03-0512.8813.451012.8793180.390.262.023.19

In [34]:

data.sort_index().head()

Out[34]:

openhighcloselowvolumeprice_changep_changeturnover
2015-03-0212.2512.671012.2096291.730.322.623.30
2015-03-0312.5213.061012.52139071.610.181.444.76
2015-03-0412.8012.921012.6167075.440.201.572.30
2015-03-0512.8813.451012.8793180.390.262.023.19
2015-03-0613.1714.481013.13179831.721.128.516.16

In [37]:

data["high"].sort_values().head()

Out[37]:

2015-03-02    12.67
2015-03-04    12.92
2015-03-03    13.06
2015-09-07    13.38
2015-03-05    13.45
Name: high, dtype: float64

In [39]:

data["high"].sort_index().head()

Out[39]:

2015-03-02    12.67
2015-03-03    13.06
2015-03-04    12.92
2015-03-05    13.45
2015-03-06    14.48
Name: high, dtype: float64

In [79]:

import pandas as pd

import matplotlib.pyplot as plt

In [35]:

data = pd.read_csv("./data/stock_day.csv")

In [84]:

data.head()

Out[84]:

openhighcloselowvolumeprice_changep_changeturnover
2015-03-0212.2512.6712.5212.2096291.730.322.623.30
2015-03-0312.5213.0612.7012.52139071.610.181.444.76
2015-03-0412.8012.9212.9012.6167075.440.201.572.30
2015-03-0512.8813.4513.1612.8793180.390.262.023.19
2015-03-0613.1714.4814.2813.13179831.721.128.516.16

In [37]:

data = data.drop(["ma5","ma10","ma20","v_ma5","v_ma10","v_ma20"], axis=1)

In [38]:

data.head()

Out[38]:

openhighcloselowvolumeprice_changep_changeturnover
2018-02-2723.5325.8824.1623.5395578.030.632.682.39
2018-02-2622.8023.7823.5322.8060985.110.693.021.53
2018-02-2322.8823.3722.8222.7152914.010.542.421.32
2018-02-2222.2522.7622.2822.0236105.010.361.640.90
2018-02-1421.4921.9921.9221.4823331.040.442.050.58

算术运算

In [41]:

data["open"].add(10).head()

Out[41]:

2018-02-27    33.53
2018-02-26    32.80
2018-02-23    32.88
2018-02-22    32.25
2018-02-14    31.49
Name: open, dtype: float64

In [43]:

# data["open"]+10 # 一般不会这么使用

逻辑运算

In [45]:

# data["close"] > 20

In [48]:

data[data["open"] > 23].head()

Out[48]:

openhighcloselowvolumeprice_changep_changeturnover
2018-02-2723.5325.8824.1623.5395578.030.632.682.39
2018-02-0123.7123.8622.4222.2266414.64-1.30-5.481.66
2018-01-3123.8523.9823.7223.3149155.02-0.11-0.461.23
2018-01-3023.7124.0823.8323.7032420.430.050.210.81
2018-01-2924.4024.6323.7723.7265469.81-0.73-2.981.64

In [51]:

data[(data["open"]>23)&(data["open"]<24)].head()

Out[51]:

openhighcloselowvolumeprice_changep_changeturnover
2018-02-2723.5325.8824.1623.5395578.030.632.682.39
2018-02-0123.7123.8622.4222.2266414.64-1.30-5.481.66
2018-01-3123.8523.9823.7223.3149155.02-0.11-0.461.23
2018-01-3023.7124.0823.8323.7032420.430.050.210.81
2018-01-1623.4024.6024.4023.30101295.420.964.102.54

In [53]:

data.query("open<24 & open>23").head()

Out[53]:

openhighcloselowvolumeprice_changep_changeturnover
2018-02-2723.5325.8824.1623.5395578.030.632.682.39
2018-02-0123.7123.8622.4222.2266414.64-1.30-5.481.66
2018-01-3123.8523.9823.7223.3149155.02-0.11-0.461.23
2018-01-3023.7124.0823.8323.7032420.430.050.210.81
2018-01-1623.4024.6024.4023.30101295.420.964.102.54

In [57]:

data[data["open"].isin([23.23, 23.71])]

Out[57]:

openhighcloselowvolumeprice_changep_changeturnover
2018-02-0123.7123.8622.4222.2266414.64-1.30-5.481.66
2018-01-3023.7124.0823.8323.7032420.430.050.210.81
2017-12-1923.2323.6623.4623.2343068.700.311.341.08

统计运算

In [58]:

data.describe()

Out[58]:

openhighcloselowvolumeprice_changep_changeturnover
count643.000000643.000000643.000000643.000000643.000000643.000000643.000000643.000000
mean21.27270621.90051321.33626720.77183599905.5191140.0188020.1902802.936190
std3.9309734.0775783.9428063.79196873879.1193540.8984764.0796982.079375
min12.25000012.67000012.36000012.2000001158.120000-3.520000-10.0300000.040000
25%19.00000019.50000019.04500018.52500048533.210000-0.390000-1.8500001.360000
50%21.44000021.97000021.45000020.98000083175.9300000.0500000.2600002.500000
75%23.40000024.06500023.41500022.850000127580.0550000.4550002.3050003.915000
max34.99000036.35000035.21000034.010000501915.4100003.03000010.03000012.560000

In [60]:

data.max(0)

Out[60]:

open                34.99
high                36.35
close               35.21
low                 34.01
volume          501915.41
price_change         3.03
p_change            10.03
turnover            12.56
dtype: float64

In [62]:

# data.max(1)

In [63]:

df = pd.DataFrame({'COL1' : [2,3,4,5,4,2],

                   'COL2' : [0,1,2,3,4,2]})

In [64]:

df

Out[64]:

COL1COL2
020
131
242
353
444
522

In [65]:

df.median()

Out[65]:

COL1    3.5
COL2    2.0
dtype: float64

In [66]:

data.idxmax()

Out[66]:

open            2015-06-15
high            2015-06-10
close           2015-06-12
low             2015-06-12
volume          2017-10-26
price_change    2015-06-09
p_change        2015-08-28
turnover        2017-10-26
dtype: object

In [67]:

data.idxmin()

Out[67]:

open            2015-03-02
high            2015-03-02
close           2015-09-02
low             2015-03-02
volume          2016-07-06
price_change    2015-06-15
p_change        2015-09-01
turnover        2016-07-06
dtype: object

In [70]:

data = data.sort_index()

In [71]:

data.head()

Out[71]:

openhighcloselowvolumeprice_changep_changeturnover
2015-03-0212.2512.6712.5212.2096291.730.322.623.30
2015-03-0312.5213.0612.7012.52139071.610.181.444.76
2015-03-0412.8012.9212.9012.6167075.440.201.572.30
2015-03-0512.8813.4513.1612.8793180.390.262.023.19
2015-03-0613.1714.4814.2813.13179831.721.128.516.16

In [73]:

stock_rise = data["p_change"]

In [75]:

# stock_rise.cumsum()

In [80]:

stock_rise.cumsum().plot()

plt.show()

自定义运算

In [88]:

data[["open", "close"]].apply(lambda x: x.max()-x.min(), axis=0)

Out[88]:

open     22.74
close    22.85
dtype: float64

In [1]:

import pandas as pd

csv

In [2]:

data = pd.read_csv("./data/stock_day.csv", usecols=["open","close"])

In [3]:

data.head()

Out[3]:

openclose
2018-02-2723.5324.16
2018-02-2622.8023.53
2018-02-2322.8822.82
2018-02-2222.2522.28
2018-02-1421.4921.92

In [4]:

# data.to_csv("./data/test.csv", columns=["close"])

data.to_csv("./data/test.csv", columns=["close"], index=False)

In [5]:

data = pd.read_csv("./data/test.csv")

In [6]:

data.head()

Out[6]:

close
024.16
123.53
222.82
322.28
421.92

hdf5

In [7]:

day_close = pd.read_hdf("./data/day_close.h5")

In [8]:

day_close.head()

Out[8]:

000001.SZ000002.SZ000004.SZ000005.SZ000006.SZ000007.SZ000008.SZ000009.SZ000010.SZ000011.SZ...001965.SZ603283.SH002920.SZ002921.SZ300684.SZ002922.SZ300735.SZ603329.SH603655.SH603080.SH
016.3017.714.582.8814.602.624.964.665.376.02...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
117.0219.204.653.0215.972.654.954.705.376.27...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
217.0217.284.563.0614.372.634.824.475.375.96...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
316.1816.974.492.9513.102.734.894.335.375.77...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
416.9517.194.552.9913.182.774.974.425.375.92...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN

5 rows × 3562 columns

In [9]:

day_close.to_hdf("./data/test.h5", key="day_close")

In [10]:

new_data = pd.read_hdf("./data/test.h5", key="day_close")

In [11]:

new_data.head()

Out[11]:

000001.SZ000002.SZ000004.SZ000005.SZ000006.SZ000007.SZ000008.SZ000009.SZ000010.SZ000011.SZ...001965.SZ603283.SH002920.SZ002921.SZ300684.SZ002922.SZ300735.SZ603329.SH603655.SH603080.SH
016.3017.714.582.8814.602.624.964.665.376.02...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
117.0219.204.653.0215.972.654.954.705.376.27...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
217.0217.284.563.0614.372.634.824.475.375.96...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
316.1816.974.492.9513.102.734.894.335.375.77...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
416.9517.194.552.9913.182.774.974.425.375.92...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN

5 rows × 3562 columns

json

In [12]:

data = pd.read_json("./data/Sarcasm_Headlines_Dataset.json", orient="records", lines=True)

In [13]:

data.head()

Out[13]:

article_linkheadlineis_sarcastic
0https://www.huffingtonpost.com/entry/versace-b...former versace store clerk sues over secret 'b...0
1https://www.huffingtonpost.com/entry/roseanne-...the 'roseanne' revival catches up to our thorn...0
2https://local.theonion.com/mom-starting-to-fea...mom starting to fear son's web series closest ...1
3https://politics.theonion.com/boehner-just-wan...boehner just wants wife to listen, not come up...1
4https://www.huffingtonpost.com/entry/jk-rowlin...j.k. rowling wishes snape happy birthday in th...0

In [14]:

data.to_json("./data/test.json", orient="records")

In [15]:

data.to_json("./data/test.json", orient="records", lines=True)


In [1]:

import pandas as pd

import numpy as np
import matplotlib.pyplot as plt

缺失值处理

In [2]:

movie = pd.read_csv("./data/IMDB-Movie-Data.csv")

In [3]:

movie.head()

Out[3]:

RankTitleGenreDescriptionDirectorActorsYearRuntime (Minutes)RatingVotesRevenue (Millions)Metascore
01Guardians of the GalaxyAction,Adventure,Sci-FiA group of intergalactic criminals are forced ...James GunnChris Pratt, Vin Diesel, Bradley Cooper, Zoe S...20141218.1757074333.1376.0
12PrometheusAdventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
23SplitHorror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0
34SingAnimation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0
45Suicide SquadAction,Adventure,FantasyA secret government agency recruits some of th...David AyerWill Smith, Jared Leto, Margot Robbie, Viola D...20161236.2393727325.0240.0

缺失值是nan

In [4]:

np.all(pd.notnull(movie)) # 里面如果有一个缺失值,那么会返回False,说明有缺失值

Out[4]:

False

In [5]:

np.any(pd.isnull(movie)) # 里面如果有一个缺失值,那么会返回True,说明有缺失值

Out[5]:

True

In [6]:

data = movie.dropna()

In [7]:

np.all(pd.notnull(data)) # 里面如果有一个缺失值,那么会返回False,说明有缺失值

Out[7]:

True

In [8]:

movie["Revenue (Millions)"].mean()

Out[8]:

82.95637614678898

In [9]:

movie["Revenue (Millions)"].fillna(movie["Revenue (Millions)"].mean(), inplace=True)

In [10]:

movie.head()

Out[10]:

RankTitleGenreDescriptionDirectorActorsYearRuntime (Minutes)RatingVotesRevenue (Millions)Metascore
01Guardians of the GalaxyAction,Adventure,Sci-FiA group of intergalactic criminals are forced ...James GunnChris Pratt, Vin Diesel, Bradley Cooper, Zoe S...20141218.1757074333.1376.0
12PrometheusAdventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
23SplitHorror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0
34SingAnimation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0
45Suicide SquadAction,Adventure,FantasyA secret government agency recruits some of th...David AyerWill Smith, Jared Leto, Margot Robbie, Viola D...20161236.2393727325.0240.0

In [11]:

for i in movie.columns:

    if np.any(pd.isnull(movie[i])) == True:
        print(i)
        movie[i].fillna(movie[i].mean(), inplace=True)
Metascore

In [12]:

np.any(pd.isnull(movie)) # 里面如果有一个缺失值,那么会返回True,说明有缺失值

Out[12]:

False

In [13]:

wis = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data")

In [14]:

wis.head()

Out[14]:

1000025511.11.221.331.41.52.1
0100294554457103212
110154253111223112
210162776881343712
310170234113213112
410171228101087109714

缺失值是其他符号

In [15]:

wis = wis.replace(to_replace="?", value=np.nan)

In [16]:

wis.head()

Out[16]:

1000025511.11.221.331.41.52.1
0100294554457103212
110154253111223112
210162776881343712
310170234113213112
410171228101087109714

In [17]:

wis = wis.dropna()

In [18]:

np.any(pd.isnull(wis)) # 里面如果有一个缺失值,那么会返回True,说明有缺失值

Out[18]:

False

数据离散化

In [19]:

data = pd.read_csv("./data/stock_day.csv")

In [20]:

data.head()

Out[20]:

openhighcloselowvolumeprice_changep_changema5ma10ma20v_ma5v_ma10v_ma20turnover
2018-02-2723.5325.8824.1623.5395578.030.632.6822.94222.14222.87553782.6446738.6555576.112.39
2018-02-2622.8023.7823.5322.8060985.110.693.0222.40621.95522.94240827.5242736.3456007.501.53
2018-02-2322.8823.3722.8222.7152914.010.542.4221.93821.92923.02235119.5841871.9756372.851.32
2018-02-2222.2522.7622.2822.0236105.010.361.6421.44621.90923.13735397.5839904.7860149.600.90
2018-02-1421.4921.9921.9221.4823331.040.442.0521.36621.92323.25333590.2142935.7461716.110.58

In [21]:

p_change = data["p_change"]

In [22]:

p_change.head()

Out[22]:

2018-02-27    2.68
2018-02-26    3.02
2018-02-23    2.42
2018-02-22    1.64
2018-02-14    2.05
Name: p_change, dtype: float64

In [23]:

# 自动分成差不多数量的类别

qcut = pd.qcut(p_change, 10)
qcut.value_counts()

Out[23]:

(5.27, 10.03]                    65
(0.26, 0.94]                     65
(-0.462, 0.26]                   65
(-10.030999999999999, -4.836]    65
(2.938, 5.27]                    64
(1.738, 2.938]                   64
(-1.352, -0.462]                 64
(-2.444, -1.352]                 64
(-4.836, -2.444]                 64
(0.94, 1.738]                    63
Name: p_change, dtype: int64

In [24]:

# 指定分组区间

bins = [-100, -7, -5, -3, 0, 3, 5, 7, 100]
p_count = pd.cut(p_change, bins)

In [25]:

p_count.value_counts()

Out[25]:

(0, 3]        215
(-3, 0]       188
(3, 5]         57
(-5, -3]       51
(7, 100]       35
(5, 7]         35
(-100, -7]     34
(-7, -5]       28
Name: p_change, dtype: int64

In [26]:

dummies = pd.get_dummies(p_count, prefix="rise")

dummies.head()

Out[26]:

rise_(-100, -7]rise_(-7, -5]rise_(-5, -3]rise_(-3, 0]rise_(0, 3]rise_(3, 5]rise_(5, 7]rise_(7, 100]
2018-02-2700001000
2018-02-2600000100
2018-02-2300001000
2018-02-2200001000
2018-02-1400001000

合并

In [27]:

pd.concat([data, dummies],axis=1)

Out[27]:

openhighcloselowvolumeprice_changep_changema5ma10ma20...v_ma20turnoverrise_(-100, -7]rise_(-7, -5]rise_(-5, -3]rise_(-3, 0]rise_(0, 3]rise_(3, 5]rise_(5, 7]rise_(7, 100]
2018-02-2723.5325.8824.1623.5395578.030.632.6822.94222.14222.875...55576.112.3900001000
2018-02-2622.8023.7823.5322.8060985.110.693.0222.40621.95522.942...56007.501.5300000100
2018-02-2322.8823.3722.8222.7152914.010.542.4221.93821.92923.022...56372.851.3200001000
2018-02-2222.2522.7622.2822.0236105.010.361.6421.44621.90923.137...60149.600.9000001000
2018-02-1421.4921.9921.9221.4823331.040.442.0521.36621.92323.253...61716.110.5800001000
2018-02-1321.4021.9021.4821.3130802.450.281.3221.34222.10323.387...65161.680.7700001000
2018-02-1220.7021.4021.1920.6332445.390.824.0321.50422.33823.533...68686.330.8100000100
2018-02-0921.2021.4620.3620.1954304.01-1.50-6.8621.92022.59623.645...70552.471.3601000000
2018-02-0821.7922.0921.8821.7527068.160.090.4122.37223.00923.839...73852.450.6800001000
2018-02-0722.6923.1121.8021.2953853.25-0.50-2.2422.48023.25823.929...74925.331.3500010000
2018-02-0622.8023.5522.2922.2055555.00-0.97-4.1722.86423.60724.029...75738.951.3900100000
2018-02-0522.4523.3923.2722.2552341.390.652.8723.17223.92824.112...77070.001.3100001000
2018-02-0222.4022.7022.6221.5333242.110.200.8923.27224.11424.184...79929.710.8300001000
2018-02-0123.7123.8622.4222.2266414.64-1.30-5.4823.64624.36524.279...88480.921.6601000000
2018-01-3123.8523.9823.7223.3149155.02-0.11-0.4624.03624.58324.411...91666.751.2300010000
2018-01-3023.7124.0823.8323.7032420.430.050.2124.35024.67124.365...92943.350.8100001000
2018-01-2924.4024.6323.7723.7265469.81-0.73-2.9824.68424.72824.294...93456.221.6400010000
2018-01-2624.2724.7424.4924.2250601.830.110.4524.95624.69424.221...91980.511.2700001000
2018-01-2524.9924.9924.3724.23104097.59-0.93-3.6825.08424.66924.109...92262.672.6100100000
2018-01-2425.4926.2825.2925.20134838.00-0.20-0.7925.13024.59923.997...89522.223.3700010000
2018-01-2325.1525.5325.5024.93104205.760.391.5524.99224.45023.844...85876.802.6100001000
2018-01-2225.1425.4025.1324.7568292.08-0.01-0.0424.77224.29623.644...84970.001.7100010000
2018-01-1924.6025.3425.1324.42128449.110.532.1524.43224.25423.537...82975.103.2100001000
2018-01-1824.4024.8824.6024.3067435.140.010.0424.25424.19223.441...78252.921.6900001000
2018-01-1724.4224.9224.6023.8092242.510.200.8224.06824.23923.378...77049.612.3100001000
2018-01-1623.4024.6024.4023.30101295.420.964.1023.90824.05823.321...74590.922.5400000100
2018-01-1524.0124.2323.4323.3069768.17-0.80-3.3023.82023.86023.257...71006.651.7500100000
2018-01-1223.7025.1524.2423.42120303.530.562.3724.07623.74823.236...69690.353.0100001000
2018-01-1123.6723.8523.6723.2148525.75-0.12-0.5024.13023.54823.197...65928.231.2100010000
2018-01-1024.1024.6023.8023.4070125.79-0.14-0.5824.41023.39423.204...66934.891.7600010000
..................................................................
2015-04-1319.6021.3021.1319.50171822.691.708.7519.22817.81216.563...111752.315.8800000001
2015-04-1019.5519.8919.4319.20112962.15-0.19-0.9718.33417.27616.230...106228.293.8700010000
2015-04-0918.2819.8919.6218.02183119.051.206.5117.73616.82615.964...104829.106.2700000010
2015-04-0817.6018.5318.4217.60157725.970.885.0217.07016.39415.698...101658.575.4000000010
2015-04-0716.5417.9817.5416.50122471.850.885.2816.62016.12015.510...98832.944.1900000010
2015-04-0316.4416.7716.6616.2591962.880.221.3416.39615.90415.348...99956.633.1500001000
2015-04-0216.2116.5016.4416.2166336.320.150.9216.21815.77215.229...104350.082.2700001000
2015-04-0116.1816.4816.2916.0068609.420.120.7415.91615.66615.065...105692.282.3500001000
2015-03-3116.7816.8816.1716.0784467.62-0.25-1.5215.71815.56814.896...105615.582.8900010000
2015-03-3015.9916.6316.4215.9985090.450.654.1215.62015.46914.722...108345.782.9100000100
2015-03-2714.9015.8615.7714.90120352.130.845.6315.41215.31414.527...108905.844.1200000010
2015-03-2615.1415.3514.9314.9184877.75-0.37-2.4215.32615.18414.462...108303.412.9100010000
2015-03-2515.9715.9715.3015.1897174.40-0.38-2.4215.41615.10214.436...109604.833.3300010000
2015-03-2415.3816.1615.6815.28153390.080.301.9515.41815.00214.385...110336.035.2500001000
2015-03-2315.3415.5615.3815.2589461.320.040.2615.31814.89914.304...107645.163.0600001000
2015-03-2015.3815.4815.3415.1876800.13-0.04-0.2615.21614.79214.232...108857.412.6300010000
2015-03-1915.2015.6415.3815.1193644.190.070.4615.04214.68614.153...111147.223.2100001000
2015-03-1815.1815.6615.3115.02121538.710.130.8614.78814.46414.058...112493.604.1600001000
2015-03-1714.9015.4415.1814.63158770.770.312.0814.58614.22313.954...111739.855.4300001000
2015-03-1614.5215.0514.8714.5194468.300.402.7614.48013.97513.843...107464.313.2300001000
2015-03-1314.1314.5014.4714.0861342.220.362.5514.36813.74013.740...108763.912.1000001000
2015-03-1214.1114.8014.1113.9584978.37-0.19-1.3314.33013.65913.659...114032.982.9100010000
2015-03-1114.8015.0814.3014.14119708.43-0.35-2.3914.14013.60313.603...117664.814.1000010000
2015-03-1014.2014.8014.6514.01101213.510.342.3813.86013.50313.503...117372.873.4600001000
2015-03-0914.1414.8514.3113.80144945.660.030.2113.47013.31213.312...120066.094.9600001000
2015-03-0613.1714.4814.2813.13179831.721.128.5113.11213.11213.112...115090.186.1600000001
2015-03-0512.8813.4513.1612.8793180.390.262.0212.82012.82012.820...98904.793.1900001000
2015-03-0412.8012.9212.9012.6167075.440.201.5712.70712.70712.707...100812.932.3000001000
2015-03-0312.5213.0612.7012.52139071.610.181.4412.61012.61012.610...117681.674.7600001000
2015-03-0212.2512.6712.5212.2096291.730.322.6212.52012.52012.520...96291.733.3000001000

643 rows × 22 columns

In [28]:

left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],

                        'key2': ['K0', 'K1', 'K0', 'K1'],
                        'A': ['A0', 'A1', 'A2', 'A3'],
                        'B': ['B0', 'B1', 'B2', 'B3']})
right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
                        'key2': ['K0', 'K0', 'K0', 'K0'],
                        'C': ['C0', 'C1', 'C2', 'C3'],
                        'D': ['D0', 'D1', 'D2', 'D3']})

In [29]:

left

Out[29]:

ABkey1key2
0A0B0K0K0
1A1B1K0K1
2A2B2K1K0
3A3B3K2K1

In [30]:

right

Out[30]:

CDkey1key2
0C0D0K0K0
1C1D1K1K0
2C2D2K1K0
3C3D3K2K0

In [31]:

pd.merge(left, right, on=["key1", "key2"])

Out[31]:

ABkey1key2CD
0A0B0K0K0C0D0
1A2B2K1K0C1D1
2A2B2K1K0C2D2

In [32]:

pd.merge(left, right, on=["key1", "key2"], how="inner")

Out[32]:

ABkey1key2CD
0A0B0K0K0C0D0
1A2B2K1K0C1D1
2A2B2K1K0C2D2

In [33]:

pd.merge(left, right, on=["key1", "key2"], how="left")

Out[33]:

ABkey1key2CD
0A0B0K0K0C0D0
1A1B1K0K1NaNNaN
2A2B2K1K0C1D1
3A2B2K1K0C2D2
4A3B3K2K1NaNNaN

In [34]:

pd.merge(left, right, on=["key1", "key2"], how="right")

Out[34]:

ABkey1key2CD
0A0B0K0K0C0D0
1A2B2K1K0C1D1
2A2B2K1K0C2D2
3NaNNaNK2K0C3D3

In [35]:

pd.merge(left, right, on=["key1", "key2"], how="outer")

Out[35]:

ABkey1key2CD
0A0B0K0K0C0D0
1A1B1K0K1NaNNaN
2A2B2K1K0C1D1
3A2B2K1K0C2D2
4A3B3K2K1NaNNaN
5NaNNaNK2K0C3D3

交叉表透视表

In [36]:

data.head()

Out[36]:

openhighcloselowvolumeprice_changep_changema5ma10ma20v_ma5v_ma10v_ma20turnover
2018-02-2723.5325.8824.1623.5395578.030.632.6822.94222.14222.87553782.6446738.6555576.112.39
2018-02-2622.8023.7823.5322.8060985.110.693.0222.40621.95522.94240827.5242736.3456007.501.53
2018-02-2322.8823.3722.8222.7152914.010.542.4221.93821.92923.02235119.5841871.9756372.851.32
2018-02-2222.2522.7622.2822.0236105.010.361.6421.44621.90923.13735397.5839904.7860149.600.90
2018-02-1421.4921.9921.9221.4823331.040.442.0521.36621.92323.25333590.2142935.7461716.110.58

In [37]:

data.index

Out[37]:

Index(['2018-02-27', '2018-02-26', '2018-02-23', '2018-02-22', '2018-02-14',
       '2018-02-13', '2018-02-12', '2018-02-09', '2018-02-08', '2018-02-07',
       ...
       '2015-03-13', '2015-03-12', '2015-03-11', '2015-03-10', '2015-03-09',
       '2015-03-06', '2015-03-05', '2015-03-04', '2015-03-03', '2015-03-02'],
      dtype='object', length=643)

In [38]:

time = pd.to_datetime(data.index)

In [39]:

time.weekday

Out[39]:

Int64Index([1, 0, 4, 3, 2, 1, 0, 4, 3, 2,
            ...
            4, 3, 2, 1, 0, 4, 3, 2, 1, 0],
           dtype='int64', length=643)

In [40]:

time.day

Out[40]:

Int64Index([27, 26, 23, 22, 14, 13, 12,  9,  8,  7,
            ...
            13, 12, 11, 10,  9,  6,  5,  4,  3,  2],
           dtype='int64', length=643)

In [41]:

data["week"] = time.weekday

In [42]:

data.head()

Out[42]:

openhighcloselowvolumeprice_changep_changema5ma10ma20v_ma5v_ma10v_ma20turnoverweek
2018-02-2723.5325.8824.1623.5395578.030.632.6822.94222.14222.87553782.6446738.6555576.112.391
2018-02-2622.8023.7823.5322.8060985.110.693.0222.40621.95522.94240827.5242736.3456007.501.530
2018-02-2322.8823.3722.8222.7152914.010.542.4221.93821.92923.02235119.5841871.9756372.851.324
2018-02-2222.2522.7622.2822.0236105.010.361.6421.44621.90923.13735397.5839904.7860149.600.903
2018-02-1421.4921.9921.9221.4823331.040.442.0521.36621.92323.25333590.2142935.7461716.110.582

In [43]:

data["p_n"] = np.where(data["p_change"] > 0, 1, 0)
In [44]:

data.head()

Out[44]:

openhighcloselowvolumeprice_changep_changema5ma10ma20v_ma5v_ma10v_ma20turnoverweekp_n
2018-02-2723.5325.8824.1623.5395578.030.632.6822.94222.14222.87553782.6446738.6555576.112.3911
2018-02-2622.8023.7823.5322.8060985.110.693.0222.40621.95522.94240827.5242736.3456007.501.5301
2018-02-2322.8823.3722.8222.7152914.010.542.4221.93821.92923.02235119.5841871.9756372.851.3241
2018-02-2222.2522.7622.2822.0236105.010.361.6421.44621.90923.13735397.5839904.7860149.600.9031
2018-02-1421.4921.9921.9221.4823331.040.442.0521.36621.92323.25333590.2142935.7461716.110.5821

In [45]:

count = pd.crosstab(data["week"], data["p_n"])

count

Out[45]:

p_n01
week
06362
15576
26171
36365
45968

In [46]:

sum = count.sum(axis=1).astype(np.float32)

sum

Out[46]:

week
0    125.0
1    131.0
2    132.0
3    128.0
4    127.0
dtype: float32

In [47]:

ret = count.div(sum, axis=0)

In [48]:

ret

Out[48]:

p_n01
week
00.5040000.496000
10.4198470.580153
20.4621210.537879
30.4921880.507812
40.4645670.535433

In [49]:

ret.plot(kind="bar", stacked=True)

plt.show()

In [50]:

data.pivot_table(["p_n"], index="week")

Out[50]:

p_n
week
00.496000
10.580153
20.537879
30.507812
40.535433

分组聚合

In [51]:

col =pd.DataFrame({'color': ['white','red','green','red','green'], 'object': ['pen','pencil','pencil','ashtray','pen'],'price1':[5.56,4.20,1.30,0.56,2.75],'price2':[4.75,4.12,1.60,0.75,3.15]})

​In [52]:

col

Out[52]:

colorobjectprice1price2
0whitepen5.564.75
1redpencil4.204.12
2greenpencil1.301.60
3redashtray0.560.75
4greenpen2.753.15

In [53]:

col.groupby(["color"])["price1"].mean()

Out[53]:

color
green    2.025
red      2.380
white    5.560
Name: price1, dtype: float64

In [54]:

col["price1"].groupby(col["color"]).mean()

Out[54]:

color
green    2.025
red      2.380
white    5.560
Name: price1, dtype: float64

In [55]:

col.groupby(["color"], as_index=False)["price1"].mean()

Out[55]:

colorprice1
0green2.025
1red2.380
2white5.560

In [56]:

starbucks = pd.read_csv("./data/starbucks/directory.csv")

In [57]:

starbucks.head()

Out[57]:

BrandStore NumberStore NameOwnership TypeStreet AddressCityState/ProvinceCountryPostcodePhone NumberTimezoneLongitudeLatitude
0Starbucks47370-257954Meritxell, 96LicensedAv. Meritxell, 96Andorra la Vella7ADAD500376818720GMT+1:00 Europe/Andorra1.5342.51
1Starbucks22331-212325Ajman Drive ThruLicensed1 Street 69, Al JarfAjmanAJAENaNNaNGMT+04:00 Asia/Dubai55.4725.42
2Starbucks47089-256771Dana MallLicensedSheikh Khalifa Bin Zayed St.AjmanAJAENaNNaNGMT+04:00 Asia/Dubai55.4725.39
3Starbucks22126-218024Twofour 54LicensedAl Salam StreetAbu DhabiAZAENaNNaNGMT+04:00 Asia/Dubai54.3824.48
4Starbucks17127-178586Al Ain TowerLicensedKhaldiya Area, Abu Dhabi IslandAbu DhabiAZAENaNNaNGMT+04:00 Asia/Dubai54.5424.51

In [58]:

count = starbucks.groupby(["Country"]).count()

In [59]:

count["Brand"].plot(kind="bar", figsize=(20, 8))

plt.show()

In [60]:

starbucks.groupby(["Country", "State/Province"]).count()

Out[60]:

BrandStore NumberStore NameOwnership TypeStreet AddressCityPostcodePhone NumberTimezoneLongitudeLatitude
CountryState/Province
AD711111111111
AEAJ22222200222
AZ484848484848720484848
DU8282828282821650828282
FU22222210222
RK33333303333
SH66666605666
UQ11111100111
ARB212121212121185212121
C7373737373737124737373
M55555520555
S33333330333
X66666660666
AT311111111111
533333333333
91414141414141413141414
AUNSW99999990999
QLD88888880888
VIC55555550555
AWAW33333303333
AZBA33333323333
SAB11111111111
BEBE44444440444
VAN11111110111
VBR22222220222
VLG101010101010101101010
WAL22222220222
BG211111100111
2344444410444
BH13161616161616210161616
.......................................
USMO188188188188188188188175188188188
MS3232323232323228323232
MT3636363636363636363636
NC338338338338338338338322338338338
ND1313131313131313131313
NE5858585858585856585858
NH2929292929292927292929
NJ261261261261261261261250261261261
NM7676767676767675767676
NV253253253253253253253230253253253
NY645645645645645645645627645645645
OH378378378378378378377357378378378
OK7979797979797976797979
OR359359359359359359359343359359359
PA357357357357357357357350357357357
RI2727272727272727272727
SC131131131131131131131125131131131
SD2525252525252525252525
TN180180180180180180180162180180180
TX10421042104210421042104210421002104210421042
UT10110110110110110110199101101101
VA432432432432432432432413432432432
VT88888888888
WA757757757757757757757738757757757
WI145145145145145145145144145145145
WV2525252525252523252525
WY2323232323232322232323
VNHN66666666666
SG1919191919191917191919
ZAGT33333332333

545 rows × 11 columns


In [2]:

import numpy as np

import pandas as pd
import matplotlib.pyplot as plt

In [3]:

movie = pd.read_csv("./data/IMDB-Movie-Data.csv")

In [4]:

movie.head()

Out[4]:

RankTitleGenreDescriptionDirectorActorsYearRuntime (Minutes)RatingVotesRevenue (Millions)Metascore
01Guardians of the GalaxyAction,Adventure,Sci-FiA group of intergalactic criminals are forced ...James GunnChris Pratt, Vin Diesel, Bradley Cooper, Zoe S...20141218.1757074333.1376.0
12PrometheusAdventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
23SplitHorror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0
34SingAnimation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0
45Suicide SquadAction,Adventure,FantasyA secret government agency recruits some of th...David AyerWill Smith, Jared Leto, Margot Robbie, Viola D...20161236.2393727325.0240.0

我们想知道这些电影数据中评分的平均分,导演的人数等信息,我们应该怎么获取?

In [8]:

movie["Rating"].mean()

Out[8]:

6.723199999999999

In [10]:

np.unique(movie["Director"]).shape[0]

Out[10]:

644

对于这一组电影数据,如果我们想Rating,Runtime (Minutes)的分布情况,应该如何呈现数据?

In [13]:

# Rating分布

​movie["Rating"].plot(kind="hist")

Out[13]:

<matplotlib.axes._subplots.AxesSubplot at 0x123e2ccf8>

In [16]:

# Rating分布

​# 1.创建画布
plt.figure(figsize=(20, 8), dpi=100)
​# 2.绘制图像
plt.hist(movie["Rating"].values, bins=20)
​# 2.1 添加刻度
max_ = movie["Rating"].max()
min_ = movie["Rating"].min()
​t1 = np.linspace(min_, max_, num=21)
​plt.xticks(t1)
​# 2.2 添加网格
plt.grid()
​# 3.显示
plt.show()

In [17]:

# Runtime (Minutes)分布

​# 1.创建画布
plt.figure(figsize=(20, 8), dpi=100)
​# 2.绘制图像
plt.hist(movie["Runtime (Minutes)"].values, bins=20)
​# 2.1 添加刻度
max_ = movie["Runtime (Minutes)"].max()
min_ = movie["Runtime (Minutes)"].min()
​t1 = np.linspace(min_, max_, num=21)
​plt.xticks(t1)
​# 2.2 添加网格
plt.grid()
​# 3.显示
plt.show()

对于这一组电影数据,如果我们希望统计电影分类(genre)的情况,应该如何处理数据?

In [22]:

# movie["Genre"]

temp_list = [i.split(",") for i in movie["Genre"]]

In [23]:

temp_list

Out[23]:

[['Action', 'Adventure', 'Sci-Fi'],
 ['Adventure', 'Mystery', 'Sci-Fi'],
 ['Horror', 'Thriller'],
 ['Animation', 'Comedy', 'Family'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Comedy', 'Drama', 'Music'],
 ['Comedy'],
 ['Action', 'Adventure', 'Biography'],
 ['Adventure', 'Drama', 'Romance'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Biography', 'Drama', 'History'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Comedy', 'Drama'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Biography', 'Drama', 'History'],
 ['Action', 'Thriller'],
 ['Biography', 'Drama'],
 ['Drama', 'Mystery', 'Sci-Fi'],
 ['Adventure', 'Drama', 'Thriller'],
 ['Drama'],
 ['Crime', 'Drama', 'Horror'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Comedy'],
 ['Action', 'Adventure', 'Drama'],
 ['Horror', 'Thriller'],
 ['Comedy'],
 ['Action', 'Adventure', 'Drama'],
 ['Comedy'],
 ['Drama', 'Thriller'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Adventure', 'Comedy'],
 ['Action', 'Horror', 'Sci-Fi'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Adventure', 'Drama', 'Sci-Fi'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Adventure', 'Western'],
 ['Comedy', 'Drama'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Drama'],
 ['Horror'],
 ['Biography', 'Drama', 'History'],
 ['Drama'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Drama', 'Thriller'],
 ['Adventure', 'Drama', 'Fantasy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Drama'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Comedy', 'Drama'],
 ['Action', 'Crime', 'Thriller'],
 ['Action', 'Crime', 'Drama'],
 ['Adventure', 'Drama', 'History'],
 ['Crime', 'Horror', 'Thriller'],
 ['Drama', 'Romance'],
 ['Comedy', 'Drama', 'Romance'],
 ['Biography', 'Drama'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Horror', 'Mystery', 'Thriller'],
 ['Crime', 'Drama', 'Mystery'],
 ['Drama', 'Romance', 'Thriller'],
 ['Drama', 'Mystery', 'Sci-Fi'],
 ['Action', 'Adventure', 'Comedy'],
 ['Drama', 'History', 'Thriller'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Drama'],
 ['Action', 'Drama', 'Thriller'],
 ['Drama', 'History'],
 ['Action', 'Drama', 'Romance'],
 ['Drama', 'Fantasy'],
 ['Drama', 'Romance'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Sci-Fi'],
 ['Adventure', 'Drama', 'War'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Comedy', 'Fantasy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Comedy', 'Drama'],
 ['Biography', 'Comedy', 'Crime'],
 ['Crime', 'Drama', 'Mystery'],
 ['Action', 'Crime', 'Thriller'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Crime', 'Drama'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Crime', 'Drama', 'Mystery'],
 ['Action', 'Crime', 'Drama'],
 ['Crime', 'Drama', 'Mystery'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Drama'],
 ['Comedy', 'Crime', 'Drama'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Comedy', 'Crime'],
 ['Animation', 'Drama', 'Fantasy'],
 ['Horror', 'Mystery', 'Sci-Fi'],
 ['Drama', 'Mystery', 'Thriller'],
 ['Crime', 'Drama', 'Thriller'],
 ['Biography', 'Crime', 'Drama'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Adventure', 'Drama', 'Sci-Fi'],
 ['Crime', 'Mystery', 'Thriller'],
 ['Action', 'Adventure', 'Comedy'],
 ['Crime', 'Drama', 'Thriller'],
 ['Comedy'],
 ['Action', 'Adventure', 'Drama'],
 ['Drama'],
 ['Drama', 'Mystery', 'Sci-Fi'],
 ['Action', 'Horror', 'Thriller'],
 ['Biography', 'Drama', 'History'],
 ['Romance', 'Sci-Fi'],
 ['Action', 'Fantasy', 'War'],
 ['Adventure', 'Drama', 'Fantasy'],
 ['Comedy'],
 ['Horror', 'Thriller'],
 ['Action', 'Biography', 'Drama'],
 ['Drama', 'Horror', 'Mystery'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Adventure', 'Drama', 'Family'],
 ['Adventure', 'Mystery', 'Sci-Fi'],
 ['Adventure', 'Comedy', 'Romance'],
 ['Action'],
 ['Action', 'Thriller'],
 ['Adventure', 'Drama', 'Family'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Adventure', 'Crime', 'Mystery'],
 ['Comedy', 'Family', 'Musical'],
 ['Adventure', 'Drama', 'Thriller'],
 ['Drama'],
 ['Adventure', 'Comedy', 'Drama'],
 ['Drama', 'Horror', 'Thriller'],
 ['Drama', 'Music'],
 ['Action', 'Crime', 'Thriller'],
 ['Crime', 'Drama', 'Thriller'],
 ['Crime', 'Drama', 'Thriller'],
 ['Drama', 'Romance'],
 ['Mystery', 'Thriller'],
 ['Mystery', 'Thriller', 'Western'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Comedy', 'Family'],
 ['Biography', 'Comedy', 'Drama'],
 ['Drama'],
 ['Drama', 'Western'],
 ['Drama', 'Mystery', 'Romance'],
 ['Comedy', 'Drama'],
 ['Action', 'Drama', 'Mystery'],
 ['Comedy'],
 ['Action', 'Adventure', 'Crime'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Adventure', 'Sci-Fi', 'Thriller'],
 ['Drama'],
 ['Action', 'Crime', 'Drama'],
 ['Drama', 'Horror', 'Mystery'],
 ['Action', 'Horror', 'Sci-Fi'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Comedy', 'Drama', 'Romance'],
 ['Action', 'Comedy', 'Fantasy'],
 ['Action', 'Comedy', 'Mystery'],
 ['Thriller', 'War'],
 ['Action', 'Comedy', 'Crime'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Adventure', 'Crime'],
 ['Action', 'Adventure', 'Thriller'],
 ['Drama', 'Fantasy', 'Romance'],
 ['Action', 'Adventure', 'Comedy'],
 ['Biography', 'Drama', 'History'],
 ['Action', 'Drama', 'History'],
 ['Action', 'Adventure', 'Thriller'],
 ['Crime', 'Drama', 'Thriller'],
 ['Animation', 'Adventure', 'Family'],
 ['Adventure', 'Horror'],
 ['Drama', 'Romance', 'Sci-Fi'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Adventure', 'Family'],
 ['Action', 'Adventure', 'Drama'],
 ['Action', 'Comedy'],
 ['Horror', 'Mystery', 'Thriller'],
 ['Action', 'Adventure', 'Comedy'],
 ['Comedy', 'Romance'],
 ['Horror', 'Mystery'],
 ['Drama', 'Family', 'Fantasy'],
 ['Sci-Fi'],
 ['Drama', 'Thriller'],
 ['Drama', 'Romance'],
 ['Drama', 'War'],
 ['Drama', 'Fantasy', 'Horror'],
 ['Crime', 'Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Drama', 'Romance'],
 ['Drama'],
 ['Crime', 'Drama', 'History'],
 ['Horror', 'Sci-Fi', 'Thriller'],
 ['Action', 'Drama', 'Sport'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Crime', 'Drama', 'Thriller'],
 ['Adventure', 'Biography', 'Drama'],
 ['Biography', 'Drama', 'Thriller'],
 ['Action', 'Comedy', 'Crime'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Drama', 'Fantasy', 'Horror'],
 ['Biography', 'Drama', 'Thriller'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Adventure', 'Mystery'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Drama', 'Horror'],
 ['Comedy', 'Drama', 'Romance'],
 ['Comedy', 'Romance'],
 ['Drama', 'Horror', 'Thriller'],
 ['Action', 'Adventure', 'Drama'],
 ['Drama'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Drama', 'Mystery'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Adventure', 'Comedy'],
 ['Drama', 'Horror'],
 ['Action', 'Comedy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Horror', 'Mystery'],
 ['Crime', 'Drama', 'Mystery'],
 ['Comedy', 'Crime'],
 ['Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Adventure', 'Family'],
 ['Horror', 'Sci-Fi', 'Thriller'],
 ['Drama', 'Fantasy', 'War'],
 ['Crime', 'Drama', 'Thriller'],
 ['Action', 'Adventure', 'Drama'],
 ['Action', 'Adventure', 'Thriller'],
 ['Action', 'Adventure', 'Drama'],
 ['Drama', 'Romance'],
 ['Biography', 'Drama', 'History'],
 ['Drama', 'Horror', 'Thriller'],
 ['Adventure', 'Comedy', 'Drama'],
 ['Action', 'Adventure', 'Romance'],
 ['Action', 'Drama', 'War'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Drama', 'Musical', 'Romance'],
 ['Drama', 'Sci-Fi', 'Thriller'],
 ['Comedy', 'Drama'],
 ['Action', 'Comedy', 'Crime'],
 ['Biography', 'Comedy', 'Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Drama', 'Thriller'],
 ['Biography', 'Drama', 'History'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Horror', 'Mystery', 'Thriller'],
 ['Comedy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Drama', 'Sci-Fi'],
 ['Horror'],
 ['Drama', 'Thriller'],
 ['Comedy', 'Drama', 'Romance'],
 ['Drama', 'Thriller'],
 ['Comedy', 'Drama'],
 ['Drama'],
 ['Action', 'Adventure', 'Comedy'],
 ['Drama', 'Horror', 'Thriller'],
 ['Comedy'],
 ['Drama', 'Sci-Fi'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Horror'],
 ['Action', 'Adventure', 'Thriller'],
 ['Adventure', 'Fantasy'],
 ['Action', 'Comedy', 'Crime'],
 ['Comedy', 'Drama', 'Music'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Adventure', 'Mystery'],
 ['Action', 'Comedy', 'Crime'],
 ['Crime', 'Drama', 'History'],
 ['Comedy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Crime', 'Mystery', 'Thriller'],
 ['Action', 'Adventure', 'Crime'],
 ['Thriller'],
 ['Biography', 'Drama', 'Romance'],
 ['Action', 'Adventure'],
 ['Action', 'Fantasy'],
 ['Action', 'Comedy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Comedy', 'Crime'],
 ['Thriller'],
 ['Action', 'Drama', 'Horror'],
 ['Comedy', 'Music', 'Romance'],
 ['Comedy'],
 ['Drama'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Drama', 'Romance'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Comedy', 'Drama'],
 ['Biography', 'Crime', 'Drama'],
 ['Drama', 'History'],
 ['Action', 'Crime', 'Thriller'],
 ['Action', 'Biography', 'Drama'],
 ['Horror'],
 ['Comedy', 'Romance'],
 ['Comedy', 'Romance'],
 ['Comedy', 'Crime', 'Drama'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Crime', 'Drama', 'Thriller'],
 ['Action', 'Crime', 'Thriller'],
 ['Comedy', 'Romance'],
 ['Biography', 'Drama', 'Sport'],
 ['Drama', 'Romance'],
 ['Drama', 'Horror'],
 ['Adventure', 'Fantasy'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Action', 'Drama', 'Sci-Fi'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Horror'],
 ['Comedy', 'Horror', 'Thriller'],
 ['Action', 'Crime', 'Thriller'],
 ['Crime', 'Drama', 'Music'],
 ['Drama'],
 ['Action', 'Crime', 'Thriller'],
 ['Action', 'Sci-Fi', 'Thriller'],
 ['Biography', 'Drama'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Drama', 'Horror', 'Sci-Fi'],
 ['Biography', 'Comedy', 'Drama'],
 ['Crime', 'Horror', 'Thriller'],
 ['Crime', 'Drama', 'Mystery'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Biography', 'Drama'],
 ['Biography', 'Drama'],
 ['Biography', 'Drama', 'History'],
 ['Action', 'Biography', 'Drama'],
 ['Drama', 'Fantasy', 'Horror'],
 ['Comedy', 'Drama', 'Romance'],
 ['Drama', 'Sport'],
 ['Drama', 'Romance'],
 ['Comedy', 'Romance'],
 ['Action', 'Crime', 'Thriller'],
 ['Action', 'Crime', 'Drama'],
 ['Action', 'Drama', 'Thriller'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Action', 'Adventure'],
 ['Action', 'Adventure', 'Romance'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Crime', 'Drama'],
 ['Comedy', 'Horror'],
 ['Comedy', 'Fantasy', 'Romance'],
 ['Drama'],
 ['Drama'],
 ['Comedy', 'Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Adventure', 'Sci-Fi', 'Thriller'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Comedy', 'Drama'],
 ['Biography', 'Drama', 'Romance'],
 ['Comedy', 'Fantasy'],
 ['Comedy', 'Drama', 'Fantasy'],
 ['Comedy'],
 ['Horror', 'Thriller'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Adventure', 'Comedy', 'Horror'],
 ['Comedy', 'Mystery'],
 ['Drama'],
 ['Adventure', 'Drama', 'Fantasy'],
 ['Drama', 'Sport'],
 ['Action', 'Adventure'],
 ['Action', 'Adventure', 'Drama'],
 ['Action', 'Drama', 'Sci-Fi'],
 ['Action', 'Mystery', 'Sci-Fi'],
 ['Action', 'Crime', 'Drama'],
 ['Action', 'Crime', 'Fantasy'],
 ['Biography', 'Comedy', 'Drama'],
 ['Action', 'Crime', 'Thriller'],
 ['Biography', 'Crime', 'Drama'],
 ['Drama', 'Sport'],
 ['Adventure', 'Comedy', 'Drama'],
 ['Action', 'Adventure', 'Thriller'],
 ['Comedy', 'Fantasy', 'Horror'],
 ['Drama', 'Sport'],
 ['Horror', 'Thriller'],
 ['Drama', 'History', 'Thriller'],
 ['Animation', 'Action', 'Adventure'],
 ['Action', 'Adventure', 'Drama'],
 ['Action', 'Comedy', 'Family'],
 ['Action', 'Adventure', 'Drama'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Comedy'],
 ['Action', 'Crime', 'Drama'],
 ['Biography', 'Drama'],
 ['Comedy', 'Romance'],
 ['Comedy'],
 ['Drama', 'Fantasy', 'Romance'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Comedy'],
 ['Comedy', 'Sci-Fi'],
 ['Comedy', 'Drama'],
 ['Animation', 'Action', 'Adventure'],
 ['Horror'],
 ['Action', 'Biography', 'Crime'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Drama', 'Romance'],
 ['Drama', 'Mystery', 'Thriller'],
 ['Drama', 'History', 'Thriller'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Adventure', 'Comedy'],
 ['Action', 'Thriller'],
 ['Comedy', 'Music'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Crime', 'Drama', 'Thriller'],
 ['Action', 'Adventure', 'Crime'],
 ['Comedy', 'Drama', 'Horror'],
 ['Drama'],
 ['Drama', 'Mystery', 'Romance'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Drama'],
 ['Action', 'Drama', 'Thriller'],
 ['Drama'],
 ['Action', 'Horror', 'Romance'],
 ['Action', 'Drama', 'Fantasy'],
 ['Action', 'Crime', 'Drama'],
 ['Drama', 'Fantasy', 'Romance'],
 ['Action', 'Crime', 'Thriller'],
 ['Action', 'Mystery', 'Thriller'],
 ['Horror', 'Mystery', 'Thriller'],
 ['Action', 'Horror', 'Sci-Fi'],
 ['Comedy', 'Drama'],
 ['Comedy'],
 ['Action', 'Adventure', 'Horror'],
 ['Action', 'Adventure', 'Thriller'],
 ['Action', 'Crime', 'Drama'],
 ['Comedy', 'Crime', 'Drama'],
 ['Drama', 'Romance'],
 ['Drama', 'Thriller'],
 ['Action', 'Comedy', 'Crime'],
 ['Comedy'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Drama', 'Romance'],
 ['Animation', 'Family', 'Fantasy'],
 ['Drama', 'Romance'],
 ['Thriller'],
 ['Adventure', 'Horror', 'Mystery'],
 ['Action', 'Sci-Fi'],
 ['Adventure', 'Comedy', 'Drama'],
 ['Animation', 'Action', 'Adventure'],
 ['Drama', 'Horror'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Comedy', 'Drama'],
 ['Action', 'Horror', 'Mystery'],
 ['Action', 'Thriller'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Comedy', 'Crime'],
 ['Comedy', 'Romance'],
 ['Drama', 'Romance'],
 ['Crime', 'Drama', 'Thriller'],
 ['Horror', 'Mystery', 'Thriller'],
 ['Biography', 'Drama'],
 ['Drama', 'Mystery', 'Sci-Fi'],
 ['Adventure', 'Comedy', 'Family'],
 ['Action', 'Adventure', 'Crime'],
 ['Action', 'Crime', 'Mystery'],
 ['Mystery', 'Thriller'],
 ['Action', 'Sci-Fi', 'Thriller'],
 ['Action', 'Comedy', 'Crime'],
 ['Biography', 'Crime', 'Drama'],
 ['Biography', 'Drama', 'History'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Biography', 'Drama', 'History'],
 ['Biography', 'Comedy', 'Drama'],
 ['Drama', 'Thriller'],
 ['Horror', 'Thriller'],
 ['Drama'],
 ['Drama', 'War'],
 ['Comedy', 'Drama', 'Romance'],
 ['Drama', 'Romance', 'Sci-Fi'],
 ['Action', 'Crime', 'Drama'],
 ['Comedy', 'Drama'],
 ['Animation', 'Action', 'Adventure'],
 ['Adventure', 'Comedy', 'Drama'],
 ['Comedy', 'Drama', 'Family'],
 ['Drama', 'Romance', 'Thriller'],
 ['Comedy', 'Crime', 'Drama'],
 ['Animation', 'Comedy', 'Family'],
 ['Drama', 'Horror', 'Sci-Fi'],
 ['Action', 'Adventure', 'Drama'],
 ['Action', 'Horror', 'Sci-Fi'],
 ['Action', 'Crime', 'Sport'],
 ['Drama', 'Horror', 'Sci-Fi'],
 ['Drama', 'Horror', 'Sci-Fi'],
 ['Action', 'Adventure', 'Comedy'],
 ['Mystery', 'Sci-Fi', 'Thriller'],
 ['Crime', 'Drama', 'Thriller'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Sci-Fi', 'Thriller'],
 ['Drama', 'Romance'],
 ['Crime', 'Drama', 'Thriller'],
 ['Comedy', 'Drama', 'Music'],
 ['Drama', 'Fantasy', 'Romance'],
 ['Crime', 'Drama', 'Thriller'],
 ['Crime', 'Drama', 'Thriller'],
 ['Comedy', 'Drama', 'Romance'],
 ['Comedy', 'Romance'],
 ['Drama', 'Sci-Fi', 'Thriller'],
 ['Drama', 'War'],
 ['Action', 'Crime', 'Drama'],
 ['Sci-Fi', 'Thriller'],
 ['Adventure', 'Drama', 'Horror'],
 ['Comedy', 'Drama', 'Music'],
 ['Comedy', 'Drama', 'Romance'],
 ['Action', 'Adventure', 'Drama'],
 ['Action', 'Crime', 'Drama'],
 ['Adventure', 'Fantasy'],
 ['Drama', 'Romance'],
 ['Biography', 'History', 'Thriller'],
 ['Crime', 'Drama', 'Thriller'],
 ['Action', 'Drama', 'History'],
 ['Biography', 'Comedy', 'Drama'],
 ['Crime', 'Drama', 'Thriller'],
 ['Action', 'Biography', 'Drama'],
 ['Action', 'Drama', 'Sci-Fi'],
 ['Adventure', 'Horror'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Adventure', 'Mystery'],
 ['Comedy', 'Drama', 'Romance'],
 ['Horror', 'Thriller'],
 ['Action', 'Sci-Fi', 'Thriller'],
 ['Action', 'Sci-Fi', 'Thriller'],
 ['Biography', 'Drama'],
 ['Action', 'Crime', 'Drama'],
 ['Action', 'Crime', 'Mystery'],
 ['Action', 'Adventure', 'Comedy'],
 ['Crime', 'Drama', 'Thriller'],
 ['Crime', 'Drama'],
 ['Mystery', 'Thriller'],
 ['Mystery', 'Sci-Fi', 'Thriller'],
 ['Action', 'Mystery', 'Sci-Fi'],
 ['Drama', 'Romance'],
 ['Drama', 'Thriller'],
 ['Drama', 'Mystery', 'Sci-Fi'],
 ['Comedy', 'Drama'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Biography', 'Drama', 'Sport'],
 ['Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Biography', 'Drama', 'Romance'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Drama', 'Sci-Fi', 'Thriller'],
 ['Drama', 'Romance', 'Thriller'],
 ['Mystery', 'Thriller'],
 ['Mystery', 'Thriller'],
 ['Action', 'Drama', 'Fantasy'],
 ['Action', 'Adventure', 'Biography'],
 ['Adventure', 'Comedy', 'Sci-Fi'],
 ['Action', 'Adventure', 'Thriller'],
 ['Fantasy', 'Horror'],
 ['Horror', 'Mystery'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Adventure', 'Drama'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Comedy', 'Drama'],
 ['Comedy', 'Drama'],
 ['Crime', 'Drama', 'Thriller'],
 ['Comedy', 'Romance'],
 ['Animation', 'Comedy', 'Family'],
 ['Comedy', 'Drama'],
 ['Comedy', 'Drama'],
 ['Biography', 'Drama', 'Sport'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Drama', 'History'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Adventure', 'Mystery'],
 ['Crime', 'Drama', 'Mystery'],
 ['Action'],
 ['Action', 'Adventure', 'Family'],
 ['Comedy', 'Romance'],
 ['Comedy', 'Drama', 'Romance'],
 ['Biography', 'Drama', 'Sport'],
 ['Action', 'Fantasy', 'Thriller'],
 ['Biography', 'Drama', 'Sport'],
 ['Action', 'Drama', 'Fantasy'],
 ['Adventure', 'Sci-Fi', 'Thriller'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Drama', 'Mystery', 'Thriller'],
 ['Drama', 'Romance'],
 ['Crime', 'Drama', 'Mystery'],
 ['Comedy', 'Romance', 'Sport'],
 ['Comedy', 'Family'],
 ['Drama', 'Horror', 'Mystery'],
 ['Action', 'Drama', 'Sport'],
 ['Action', 'Adventure', 'Comedy'],
 ['Drama', 'Mystery', 'Sci-Fi'],
 ['Animation', 'Action', 'Comedy'],
 ['Action', 'Crime', 'Drama'],
 ['Action', 'Crime', 'Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Animation', 'Action', 'Adventure'],
 ['Crime', 'Drama'],
 ['Drama'],
 ['Drama'],
 ['Comedy', 'Crime'],
 ['Drama'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Drama', 'Fantasy', 'Romance'],
 ['Comedy', 'Drama'],
 ['Drama', 'Fantasy', 'Thriller'],
 ['Biography', 'Crime', 'Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Action', 'Crime', 'Drama'],
 ['Sci-Fi'],
 ['Action', 'Biography', 'Drama'],
 ['Action', 'Comedy', 'Romance'],
 ['Adventure', 'Comedy', 'Drama'],
 ['Comedy', 'Crime', 'Drama'],
 ['Action', 'Fantasy', 'Horror'],
 ['Drama', 'Horror'],
 ['Horror'],
 ['Action', 'Thriller'],
 ['Action', 'Adventure', 'Mystery'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Comedy', 'Drama', 'Romance'],
 ['Crime', 'Drama', 'Mystery'],
 ['Adventure', 'Comedy', 'Family'],
 ['Comedy', 'Drama', 'Romance'],
 ['Comedy'],
 ['Comedy', 'Drama', 'Horror'],
 ['Drama', 'Horror', 'Thriller'],
 ['Animation', 'Adventure', 'Family'],
 ['Comedy', 'Romance'],
 ['Mystery', 'Romance', 'Sci-Fi'],
 ['Crime', 'Drama'],
 ['Drama', 'Horror', 'Mystery'],
 ['Comedy'],
 ['Biography', 'Drama'],
 ['Comedy', 'Drama', 'Thriller'],
 ['Comedy', 'Western'],
 ['Drama', 'History', 'War'],
 ['Drama', 'Horror', 'Sci-Fi'],
 ['Drama'],
 ['Comedy', 'Drama'],
 ['Fantasy', 'Horror', 'Thriller'],
 ['Drama', 'Romance'],
 ['Action', 'Comedy', 'Fantasy'],
 ['Drama', 'Horror', 'Musical'],
 ['Crime', 'Drama', 'Mystery'],
 ['Horror', 'Mystery', 'Thriller'],
 ['Comedy', 'Music'],
 ['Drama'],
 ['Biography', 'Crime', 'Drama'],
 ['Drama'],
 ['Action', 'Adventure', 'Comedy'],
 ['Crime', 'Drama', 'Mystery'],
 ['Drama'],
 ['Action', 'Comedy', 'Crime'],
 ['Comedy', 'Drama', 'Romance'],
 ['Crime', 'Drama', 'Mystery'],
 ['Action', 'Comedy', 'Crime'],
 ['Drama'],
 ['Drama', 'Romance'],
 ['Crime', 'Drama', 'Mystery'],
 ['Adventure', 'Comedy', 'Romance'],
 ['Comedy', 'Crime', 'Drama'],
 ['Adventure', 'Drama', 'Thriller'],
 ['Biography', 'Crime', 'Drama'],
 ['Crime', 'Drama', 'Thriller'],
 ['Drama', 'History', 'Thriller'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Comedy'],
 ['Horror'],
 ['Action', 'Crime', 'Mystery'],
 ['Comedy', 'Romance'],
 ['Comedy'],
 ['Action', 'Drama', 'Thriller'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Drama', 'Mystery', 'Thriller'],
 ['Comedy', 'Drama', 'Romance'],
 ['Action', 'Fantasy', 'Horror'],
 ['Drama', 'Romance'],
 ['Biography', 'Drama'],
 ['Biography', 'Drama'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Drama', 'Mystery', 'Thriller'],
 ['Action', 'Horror', 'Sci-Fi'],
 ['Drama', 'Romance'],
 ['Biography', 'Drama'],
 ['Action', 'Adventure', 'Drama'],
 ['Adventure', 'Drama', 'Fantasy'],
 ['Drama', 'Family'],
 ['Comedy', 'Drama', 'Romance'],
 ['Drama', 'Romance', 'Sci-Fi'],
 ['Action', 'Adventure', 'Thriller'],
 ['Comedy', 'Romance'],
 ['Crime', 'Drama', 'Horror'],
 ['Comedy', 'Fantasy'],
 ['Action', 'Comedy', 'Crime'],
 ['Adventure', 'Drama', 'Romance'],
 ['Action', 'Crime', 'Drama'],
 ['Crime', 'Horror', 'Thriller'],
 ['Romance', 'Sci-Fi', 'Thriller'],
 ['Comedy', 'Drama', 'Romance'],
 ['Crime', 'Drama'],
 ['Crime', 'Drama', 'Mystery'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Animation', 'Fantasy'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Drama', 'Mystery', 'War'],
 ['Comedy', 'Romance'],
 ['Animation', 'Comedy', 'Family'],
 ['Comedy'],
 ['Horror', 'Mystery', 'Thriller'],
 ['Action', 'Adventure', 'Drama'],
 ['Comedy'],
 ['Drama'],
 ['Adventure', 'Biography', 'Drama'],
 ['Comedy'],
 ['Horror', 'Thriller'],
 ['Action', 'Drama', 'Family'],
 ['Comedy', 'Fantasy', 'Horror'],
 ['Comedy', 'Romance'],
 ['Drama', 'Mystery', 'Romance'],
 ['Action', 'Adventure', 'Comedy'],
 ['Thriller'],
 ['Comedy'],
 ['Adventure', 'Comedy', 'Sci-Fi'],
 ['Comedy', 'Drama', 'Fantasy'],
 ['Mystery', 'Thriller'],
 ['Comedy', 'Drama'],
 ['Adventure', 'Drama', 'Family'],
 ['Horror', 'Thriller'],
 ['Action', 'Drama', 'Romance'],
 ['Drama', 'Romance'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Comedy'],
 ['Action', 'Biography', 'Drama'],
 ['Drama', 'Mystery', 'Romance'],
 ['Adventure', 'Drama', 'Western'],
 ['Drama', 'Music', 'Romance'],
 ['Comedy', 'Romance', 'Western'],
 ['Thriller'],
 ['Comedy', 'Drama', 'Romance'],
 ['Horror', 'Thriller'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Crime', 'Drama', 'Mystery'],
 ['Horror', 'Mystery'],
 ['Comedy', 'Crime', 'Drama'],
 ['Action', 'Comedy', 'Romance'],
 ['Biography', 'Drama', 'History'],
 ['Adventure', 'Drama'],
 ['Drama', 'Thriller'],
 ['Drama'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Biography', 'Drama'],
 ['Drama', 'Music'],
 ['Comedy', 'Drama'],
 ['Drama', 'Thriller', 'War'],
 ['Action', 'Mystery', 'Thriller'],
 ['Horror', 'Sci-Fi', 'Thriller'],
 ['Comedy', 'Drama', 'Romance'],
 ['Action', 'Sci-Fi'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Drama', 'Mystery', 'Romance'],
 ['Drama'],
 ['Action', 'Adventure', 'Thriller'],
 ['Action', 'Crime', 'Thriller'],
 ['Animation', 'Action', 'Adventure'],
 ['Drama', 'Fantasy', 'Mystery'],
 ['Drama', 'Sci-Fi'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Horror', 'Thriller'],
 ['Action', 'Thriller'],
 ['Comedy'],
 ['Biography', 'Drama'],
 ['Action', 'Mystery', 'Thriller'],
 ['Action', 'Mystery', 'Sci-Fi'],
 ['Crime', 'Drama', 'Thriller'],
 ['Comedy', 'Romance'],
 ['Comedy', 'Drama', 'Romance'],
 ['Biography', 'Drama', 'Thriller'],
 ['Drama'],
 ['Action', 'Adventure', 'Family'],
 ['Animation', 'Comedy', 'Family'],
 ['Action', 'Crime', 'Drama'],
 ['Comedy'],
 ['Comedy', 'Crime', 'Thriller'],
 ['Comedy', 'Romance'],
 ['Animation', 'Comedy', 'Drama'],
 ['Action', 'Crime', 'Thriller'],
 ['Comedy', 'Romance'],
 ['Adventure', 'Biography', 'Drama'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Crime', 'Drama', 'Mystery'],
 ['Action', 'Comedy', 'Sci-Fi'],
 ['Comedy', 'Fantasy', 'Horror'],
 ['Comedy', 'Crime'],
 ['Animation', 'Action', 'Adventure'],
 ['Action', 'Drama', 'Thriller'],
 ['Fantasy', 'Horror'],
 ['Crime', 'Drama', 'Thriller'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Comedy', 'Drama', 'Romance'],
 ['Biography', 'Drama', 'Romance'],
 ['Action', 'Drama', 'History'],
 ['Action', 'Adventure', 'Comedy'],
 ['Horror', 'Thriller'],
 ['Horror', 'Mystery', 'Thriller'],
 ['Comedy', 'Romance'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Crime', 'Drama', 'Mystery'],
 ['Crime', 'Drama', 'Mystery'],
 ['Adventure', 'Biography', 'Drama'],
 ['Horror', 'Mystery', 'Thriller'],
 ['Horror', 'Thriller'],
 ['Drama', 'Romance', 'War'],
 ['Adventure', 'Fantasy', 'Mystery'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Biography', 'Drama'],
 ['Drama', 'Thriller'],
 ['Horror', 'Thriller'],
 ['Drama', 'Horror', 'Thriller'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Horror', 'Thriller'],
 ['Comedy'],
 ['Drama', 'Sport'],
 ['Comedy', 'Family'],
 ['Drama', 'Romance'],
 ['Action', 'Adventure', 'Comedy'],
 ['Comedy'],
 ['Mystery', 'Romance', 'Thriller'],
 ['Crime', 'Drama'],
 ['Action', 'Comedy'],
 ['Crime', 'Drama', 'Mystery'],
 ['Biography', 'Drama', 'Romance'],
 ['Comedy', 'Crime'],
 ['Drama', 'Thriller'],
 ['Drama'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Action', 'Thriller'],
 ['Drama', 'Thriller'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Crime', 'Drama', 'Mystery'],
 ['Thriller'],
 ['Biography', 'Drama', 'Sport'],
 ['Crime', 'Drama', 'Thriller'],
 ['Drama', 'Music'],
 ['Crime', 'Drama', 'Thriller'],
 ['Drama', 'Romance'],
 ['Animation', 'Action', 'Adventure'],
 ['Comedy', 'Drama'],
 ['Action', 'Adventure', 'Drama'],
 ['Biography', 'Crime', 'Drama'],
 ['Horror'],
 ['Biography', 'Drama', 'Mystery'],
 ['Drama', 'Romance'],
 ['Animation', 'Drama', 'Romance'],
 ['Comedy', 'Family'],
 ['Drama'],
 ['Mystery', 'Thriller'],
 ['Drama', 'Fantasy', 'Horror'],
 ['Drama', 'Romance'],
 ['Biography', 'Drama', 'History'],
 ['Comedy', 'Family'],
 ['Action', 'Adventure', 'Thriller'],
 ['Comedy', 'Drama'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Thriller'],
 ['Drama', 'Romance'],
 ['Comedy', 'Drama', 'Romance'],
 ['Drama', 'Horror', 'Sci-Fi'],
 ['Comedy', 'Horror', 'Romance'],
 ['Drama'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Action', 'Adventure', 'Drama'],
 ['Biography', 'Comedy', 'Drama'],
 ['Drama', 'Mystery', 'Romance'],
 ['Animation', 'Adventure', 'Comedy'],
 ['Drama', 'Romance', 'Sci-Fi'],
 ['Drama'],
 ['Drama', 'Fantasy'],
 ['Drama', 'Romance'],
 ['Comedy', 'Horror', 'Thriller'],
 ['Comedy', 'Drama', 'Romance'],
 ['Crime', 'Drama'],
 ['Comedy', 'Romance'],
 ['Action', 'Drama', 'Family'],
 ['Comedy', 'Drama', 'Romance'],
 ['Action', 'Thriller', 'War'],
 ['Action', 'Comedy', 'Horror'],
 ['Biography', 'Drama', 'Sport'],
 ['Adventure', 'Comedy', 'Drama'],
 ['Comedy', 'Romance'],
 ['Comedy', 'Romance'],
 ['Comedy', 'Drama', 'Romance'],
 ['Action', 'Adventure', 'Crime'],
 ['Comedy', 'Romance'],
 ['Animation', 'Action', 'Adventure'],
 ['Action', 'Crime', 'Sci-Fi'],
 ['Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Crime', 'Thriller'],
 ['Comedy', 'Horror', 'Sci-Fi'],
 ['Drama', 'Thriller'],
 ['Drama', 'Fantasy', 'Horror'],
 ['Thriller'],
 ['Adventure', 'Drama', 'Family'],
 ['Mystery', 'Sci-Fi', 'Thriller'],
 ['Biography', 'Crime', 'Drama'],
 ['Drama', 'Fantasy', 'Horror'],
 ['Action', 'Adventure', 'Thriller'],
 ['Crime', 'Drama', 'Horror'],
 ['Crime', 'Drama', 'Fantasy'],
 ['Adventure', 'Family', 'Fantasy'],
 ['Action', 'Adventure', 'Drama'],
 ['Action', 'Comedy', 'Horror'],
 ['Comedy', 'Drama', 'Family'],
 ['Action', 'Thriller'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Adventure', 'Drama', 'Fantasy'],
 ['Drama'],
 ['Drama'],
 ['Comedy'],
 ['Drama'],
 ['Comedy', 'Drama', 'Music'],
 ['Drama', 'Fantasy', 'Music'],
 ['Drama'],
 ['Thriller'],
 ['Comedy', 'Horror'],
 ['Action', 'Comedy', 'Sport'],
 ['Horror'],
 ['Comedy', 'Drama'],
 ['Action', 'Drama', 'Thriller'],
 ['Drama', 'Romance'],
 ['Horror', 'Mystery'],
 ['Adventure', 'Drama', 'Fantasy'],
 ['Thriller'],
 ['Comedy', 'Romance'],
 ['Action', 'Sci-Fi', 'Thriller'],
 ['Fantasy', 'Mystery', 'Thriller'],
 ['Biography', 'Drama'],
 ['Crime', 'Drama'],
 ['Action', 'Adventure', 'Sci-Fi'],
 ['Adventure'],
 ['Comedy', 'Drama'],
 ['Comedy', 'Drama'],
 ['Comedy', 'Drama', 'Romance'],
 ['Adventure', 'Comedy', 'Drama'],
 ['Action', 'Sci-Fi', 'Thriller'],
 ['Comedy', 'Romance'],
 ['Action', 'Fantasy', 'Horror'],
 ['Crime', 'Drama', 'Thriller'],
 ['Action', 'Drama', 'Thriller'],
 ['Crime', 'Drama', 'Mystery'],
 ['Crime', 'Drama', 'Mystery'],
 ['Drama', 'Sci-Fi', 'Thriller'],
 ['Biography', 'Drama', 'History'],
 ['Crime', 'Horror', 'Thriller'],
 ['Drama'],
 ['Drama', 'Mystery', 'Thriller'],
 ['Adventure', 'Biography'],
 ['Adventure', 'Biography', 'Crime'],
 ['Action', 'Horror', 'Thriller'],
 ['Action', 'Adventure', 'Western'],
 ['Horror', 'Thriller'],
 ['Drama', 'Mystery', 'Thriller'],
 ['Comedy', 'Drama', 'Musical'],
 ['Horror', 'Mystery'],
 ['Biography', 'Drama', 'Sport'],
 ['Comedy', 'Family', 'Romance'],
 ['Drama', 'Mystery', 'Thriller'],
 ['Comedy'],
 ['Drama'],
 ['Drama', 'Thriller'],
 ['Biography', 'Drama', 'Family'],
 ['Comedy', 'Drama', 'Family'],
 ['Drama', 'Fantasy', 'Musical'],
 ['Comedy'],
 ['Adventure', 'Family'],
 ['Adventure', 'Comedy', 'Fantasy'],
 ['Horror', 'Thriller'],
 ['Drama', 'Romance'],
 ['Horror'],
 ['Biography', 'Drama', 'History'],
 ['Action', 'Adventure', 'Fantasy'],
 ['Drama', 'Family', 'Music'],
 ['Comedy', 'Drama', 'Romance'],
 ['Action', 'Adventure', 'Horror'],
 ['Comedy'],
 ['Crime', 'Drama', 'Mystery'],
 ['Horror'],
 ['Drama', 'Music', 'Romance'],
 ['Adventure', 'Comedy'],
 ['Comedy', 'Family', 'Fantasy']]

In [42]:

genre_list = np.unique([i for j in temp_list for i in j])

genre_list

Out[42]:

array(['Action', 'Adventure', 'Animation', 'Biography', 'Comedy', 'Crime',
       'Drama', 'Family', 'Fantasy', 'History', 'Horror', 'Music',
       'Musical', 'Mystery', 'Romance', 'Sci-Fi', 'Sport', 'Thriller',
       'War', 'Western'], dtype='<U9')

In [29]:

zeros = np.zeros([movie.shape[0], genre_list.shape[0]])

In [30]:

temp_movie = pd.DataFrame(zeros, columns=genre_list)

In [32]:

temp_movie.head()

Out[32]:

ActionAdventureAnimationBiographyComedyCrimeDramaFamilyFantasyHistoryHorrorMusicMusicalMysteryRomanceSci-FiSportThrillerWarWestern
00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
10.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
20.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
30.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0
40.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.0

In [33]:

for i in range(1000):

    temp_movie.ix[i, temp_list[i]] = 1

In [35]:

temp_movie.head()

Out[35]:

ActionAdventureAnimationBiographyComedyCrimeDramaFamilyFantasyHistoryHorrorMusicMusicalMysteryRomanceSci-FiSportThrillerWarWestern
01.01.00.00.00.00.00.00.00.00.00.00.00.00.00.01.00.00.00.00.0
10.01.00.00.00.00.00.00.00.00.00.00.00.01.00.01.00.00.00.00.0
20.00.00.00.00.00.00.00.00.00.01.00.00.00.00.00.00.01.00.00.0
30.00.01.00.01.00.00.01.00.00.00.00.00.00.00.00.00.00.00.00.0
41.01.00.00.00.00.00.00.01.00.00.00.00.00.00.00.00.00.00.00.0

In [38]:

genre = temp_movie.sum().sort_values(ascending=False)

genre

Out[38]:

Drama        513.0
Action       303.0
Comedy       279.0
Adventure    259.0
Thriller     195.0
Crime        150.0
Romance      141.0
Sci-Fi       120.0
Horror       119.0
Mystery      106.0
Fantasy      101.0
Biography     81.0
Family        51.0
Animation     49.0
History       29.0
Sport         18.0
Music         16.0
War           13.0
Western        7.0
Musical        5.0
dtype: float64

In [41]:

genre.plot(kind="bar", colormap="cool", figsize=(20, 8), fontsize=16)

Out[41]:

<matplotlib.axes._subplots.AxesSubplot at 0x128bdea58>

 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

あずにゃん

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值