np的concatenate和pandas的groupby
Posted loubin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了np的concatenate和pandas的groupby相关的知识,希望对你有一定的参考价值。
1. concatenate
concatenate函数可以实现对两个张量进行拼接,这个张量可以实一维向量,二维矩阵等等
1. 首先定义四个列表,然后用concatenate把他们拼接起来,这里我设axis=0
name = [‘jack‘, ‘ross‘, ‘john‘, ‘blues‘, ‘frank‘, ‘bitch‘, ‘haha‘, ‘asd‘, ‘loubin‘] age = [12, 32, 23, 4,32,45,65,23,65] married = [1, 0, 1, 1, 0, 1, 0, 0, 0] gender = [0, 0, 0, 0, 1, 1, 1, 1, 1] matrix = np.concatenate((name, age, married, gender), axis=0) print(matrix)
运行结果如下
C:softwareAnacondaenvsmlpython.exe C:/学习/python/科比生涯数据分析/venv/groupy.py [‘jack‘ ‘ross‘ ‘john‘ ‘blues‘ ‘frank‘ ‘bitch‘ ‘haha‘ ‘asd‘ ‘loubin‘ ‘12‘ ‘32‘ ‘23‘ ‘4‘ ‘32‘ ‘45‘ ‘65‘ ‘23‘ ‘65‘ ‘1‘ ‘0‘ ‘1‘ ‘1‘ ‘0‘ ‘1‘ ‘0‘ ‘0‘ ‘0‘ ‘0‘ ‘0‘ ‘0‘ ‘0‘ ‘1‘ ‘1‘ ‘1‘ ‘1‘ ‘1‘]
达到的效果是直接把四个列表给拼接成了一个大的列表,长度是36。下面我们尝试用axis=1来拼接
name = [‘jack‘, ‘ross‘, ‘john‘, ‘blues‘, ‘frank‘, ‘bitch‘, ‘haha‘, ‘asd‘, ‘loubin‘] age = [12, 32, 23, 4,32,45,65,23,65] married = [1, 0, 1, 1, 0, 1, 0, 0, 0] gender = [0, 0, 0, 0, 1, 1, 1, 1, 1] matrix = np.concatenate((name, age, married, gender), axis=1)
运行结果报错如下
C:softwareAnacondaenvsmlpython.exe C:/学习/python/科比生涯数据分析/venv/groupy.py Traceback (most recent call last): File "C:/学习/python/科比生涯数据分析/venv/groupy.py", line 15, in <module> matrix = np.concatenate((name, age, married, gender), axis=1) numpy.AxisError: axis 1 is out of bounds for array of dimension 1
原因很简单,运行name.shape就可以发现,上面的列表shape属性是(9, ),也就是说他们的shape[0] 是9, 而shape[1]不存在,所以axis=1是对不存在的维度进行
操作。没错axis = k 就可以理解为对shape[k]所代表的维度进行操作。下面我们来验证以下
将以上的列表变成(1, 9)的矩阵,用numpy完成
name = np.array([[‘jack‘, ‘ross‘, ‘john‘, ‘blues‘, ‘frank‘, ‘bitch‘, ‘haha‘, ‘asd‘, ‘loubin‘]]) age = np.array([[12, 32, 23, 4,32,45,65,23,65]]) married = np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0]]) gender = np.array([[0, 0, 0, 0, 1, 1, 1, 1, 1]]) matrix = np.concatenate((name, age, married, gender), axis=0) print(matrix)
运行结果如下,因为这个时候, name,age, married, gender的shape都是(1, 9),所以axis=0时,拼接对shape[0]操作,结果就是(4, 9)的矩阵
C:softwareAnacondaenvsmlpython.exe C:/学习/python/科比生涯数据分析/venv/groupy.py [[‘jack‘ ‘ross‘ ‘john‘ ‘blues‘ ‘frank‘ ‘bitch‘ ‘haha‘ ‘asd‘ ‘loubin‘] [‘12‘ ‘32‘ ‘23‘ ‘4‘ ‘32‘ ‘45‘ ‘65‘ ‘23‘ ‘65‘] [‘1‘ ‘0‘ ‘1‘ ‘1‘ ‘0‘ ‘1‘ ‘0‘ ‘0‘ ‘0‘] [‘0‘ ‘0‘ ‘0‘ ‘0‘ ‘1‘ ‘1‘ ‘1‘ ‘1‘ ‘1‘]]
试一下将axis改成1,那么结果因该就是(1, 36)的矩阵了
name = np.array([[‘jack‘, ‘ross‘, ‘john‘, ‘blues‘, ‘frank‘, ‘bitch‘, ‘haha‘, ‘asd‘, ‘loubin‘]]) age = np.array([[12, 32, 23, 4,32,45,65,23,65]]) married = np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0]]) gender = np.array([[0, 0, 0, 0, 1, 1, 1, 1, 1]]) matrix = np.concatenate((name, age, married, gender), axis=1)
[[‘jack‘ ‘ross‘ ‘john‘ ‘blues‘ ‘frank‘ ‘bitch‘ ‘haha‘ ‘asd‘ ‘loubin‘ ‘12‘ ‘32‘ ‘23‘ ‘4‘ ‘32‘ ‘45‘ ‘65‘ ‘23‘ ‘65‘ ‘1‘ ‘0‘ ‘1‘ ‘1‘ ‘0‘ ‘1‘ ‘0‘ ‘0‘ ‘0‘ ‘0‘ ‘0‘ ‘0‘ ‘0‘ ‘1‘ ‘1‘ ‘1‘ ‘1‘ ‘1‘]]
2.groupby函数
groupyby可以接受datafram的列名作为参数,将原始数据按照列名进行分组。利用第一部分的数据说明
import pandas as pd import numpy as np import matplotlib.pyplot as plt name = np.array([[‘jack‘, ‘ross‘, ‘john‘, ‘blues‘, ‘frank‘, ‘bitch‘, ‘haha‘, ‘asd‘, ‘loubin‘]]) age = np.array([[12, 32, 23, 4,32,45,65,23,65]]) married = np.array([[1, 0, 1, 1, 0, 1, 0, 0, 0]]) gender = np.array([[0, 0, 0, 0, 1, 1, 1, 1, 1]]) matrix = np.concatenate((name, age, married, gender), axis=0) matrix = matrix.T data = pd.DataFrame(data=matrix, columns=[‘name‘, ‘age‘, ‘married‘, ‘gender‘]) print(data)
运行结果如下,生成了一个datafram
C:softwareAnacondaenvsmlpython.exe C:/学习/python/科比生涯数据分析/venv/groupy.py name age married gender 0 jack 12 1 0 1 ross 32 0 0 2 john 23 1 0 3 blues 4 1 0 4 frank 32 0 1 5 bitch 45 1 1 6 haha 65 0 1 7 asd 23 0 1 8 loubin 65 0 1
在上面的代码基础上再增加以下代码
gs = data.groupby(‘gender‘) print(len(gs)) for g in gs: print(g)
运行结果如下
2 (‘0‘, name age married gender 0 jack 12 1 0 1 ross 32 0 0 2 john 23 1 0 3 blues 4 1 0) (‘1‘, name age married gender 4 frank 32 0 1 5 bitch 45 1 1 6 haha 65 0 1 7 asd 23 0 1 8 loubin 65 0 1)
通过data.groupy(‘gender‘)生成了一个分类器gs,但是gs不能直接展示数据,要通过for循环来获取gs中的数据。这里运行len(gs)可以发现gs的长度时2,因为gender
属性只有两个值,所以gs的长度就是类别数。然后对于gs中的每一个g,是一个元组,由两部分组成,第一部分是类别值,第二部分是该类别下的datafram数据集。
以上是关于np的concatenate和pandas的groupby的主要内容,如果未能解决你的问题,请参考以下文章
为啥将 np.concatenate 与 for 循环和列表理解一起使用的结果不一样?
numpy使用np.concatenate函数将两个一维的numpy数组横向拼接起来(concatenate two 1D numpy arrays)