python数据分析之pandas(9)数据拼接

1.拼接

numpy的concatenate函数
利用concatenate可实现按axis进行拼接,如:

>>> import numpy as np
>>> a = np.arange(9)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> a = np.arange(9).reshape(3,3)
>>> b = np.arange(10, 19).reshape(3,3)
>>> b
array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])
>>> np.concatenate([a,b], axis=0)
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [10, 11, 12],
       [13, 14, 15],
       [16, 17, 18]])
>>> np.concatenate([a,b], axis=1)
array([[ 0,  1,  2, 10, 11, 12],
       [ 3,  4,  5, 13, 14, 15],
       [ 6,  7,  8, 16, 17, 18]])
>>>

可以看出,如果指定axis为0,则按行的方向合并增加,axis=1,按列的方向合并增加
(2)Series和DataFrame的concat函数
使用方式类似,不过这里和axis=0为index索引,axis=1即为columns列。
Series合并示例:

>>> import pandas as pd
>>> ser1 = pd.Series(np.arange(3), index=[1,2,3])
>>> ser1
1    0
2    1
3    2
dtype: int32
>>> ser2 = pd.Series(np.arange(8,11), index=[2,6,7])
>>> pd.concat([ser1,ser2])
1     0
2     1
3     2
2     8
6     9
7    10
dtype: int32

可以看出,直接在索引方向增加了行

若不指定索引,则会有默认索引,拼接示例如下:

>>> ser2 = pd.Series(np.arange(8,11))
>>> ser1 = pd.Series(np.arange(3))
>>> pd.concat([ser1,ser2])
0     0
1     1
2     2
0     8
1     9
2    10
dtype: int32
>>>

Series按列合并,同时指定层级列名(防止重复列名)

>>> pd.concat([ser1,ser2], keys=[1,2], axis=1)
   1   2
0  0   8
1  1   9
2  2  10
>>>

可以看到,相同的索引会复用,否则会单独作为一行添加,之前不存在的部分用NaN填充,如下所示:

>>> ser1 = pd.Series(np.arange(3))
>>> ser2 = pd.Series(np.arange(8, 11), index=[2,4,5])
>>> pd.concat([ser1,ser2], keys=[1,2], axis=1)
     1     2
0  0.0   NaN
1  1.0   NaN
2  2.0   8.0
4  NaN   9.0
5  NaN  10.0
>>>

DataFrame的拼接方式类似

numpy随机数
np随机数用法举例如下:

>>> np.random.rand(4)
array([0.09658378, 0.11693135, 0.76206127, 0.82363783])
>>> np.random.randint(4)
3
>>> np.random.randint(4, 10)
8
>>> np.random.randint(4, 10, 3)
array([8, 6, 5])
>>> np.random.permutation(5)
array([3, 1, 4, 0, 2])
>>>

可以看出:
rand(n) //返回n个0-1的浮点数
randint(n) //n内的随机整数,0-(n-1)注意和random.randint(n,m) 不一样,后者返回为(n,m]包含m的随机数
randint(n,m) //n-m范围内的随机整数
randint(n,m,k) //k个
permutation(n) //n范围内的整数随机排列返回

2. 组合

当2个合并对象存在index重合时,可以通过combine_first去掉第二个对象中的重复index

ser1.combine_first(ser2)
ser1[:3].combine_first(ser2[:3]) //部分合并

3. 轴向旋转

stack与unstack
ser = frame.stack() //DataFrame对象堆叠为Series对象,索引为层级索引。

>>> frame = pd.DataFrame(np.arange(9).reshape(3,3), index=['ball', 'pen', 'penci
l'], columns=['red', 'white', 'black'])
>>> frame
        red  white  black
ball      0      1      2
pen       3      4      5
pencil    6      7      8
>>> ser = frame.stack()
>>> ser
ball    red      0
        white    1
        black    2
pen     red      3
        white    4
        black    5
pencil  red      6
        white    7
        black    8
dtype: int32
>>>

ser.ustack() //还原展开为DataFrame

从“长”格式向“宽”格式旋转
通过pivot()函数实现转换,可以多列元素(要求多列组成的元素唯一):

>>> longframe = pd.DataFrame({'color': ['w', 'w', 'r', 'r'], 'item': ['ball', 'p
en', 'ball', 'pen'], 'value': np.arange(4)})
>>> longframe
  color  item  value
0     w  ball      0
1     w   pen      1
2     r  ball      2
3     r   pen      3
>>> longframe.pivot('color', 'item')
      value
item   ball pen
color
r         2   3
w         0   1
>>>

4.删除

del frame['ball'] //删除一列
frame.drop('white') //删除索引所在的一行,这里的white为索引名,可为int/str类型
frame.drop('white', axis=1) //删除一列,这里的white为列名
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值