栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

09-Pandas数据规整

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

09-Pandas数据规整

层次化索引
import numpy as np
import pandas as pd
data = pd.Series(np.random.randn(9),
                index=[['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd'],
                [1, 2, 3, 1, 3, 1, 2, 2, 3]])
data
a  1    1.222970
   2   -0.235655
   3    1.304342
b  1    1.415799
   3    0.119285
c  1    2.678416
   2    0.382792
d  2   -0.291932
   3   -0.687770
dtype: float64
data['a']
1    1.222970
2   -0.235655
3    1.304342
dtype: float64
data['b':'c']
b  1    1.415799
   3    0.119285
c  1    2.678416
   2    0.382792
dtype: float64
data.loc[['b','d']]
b  1    1.415799
   3    0.119285
d  2   -0.291932
   3   -0.687770
dtype: float64
data.loc[:,2]
a   -0.235655
c    0.382792
d   -0.291932
dtype: float64
frame = pd.Dataframe({'a': range(7), 'b': range(7, 0, -1),
                    'c': ['one', 'one', 'one', 'two', 'two','two', 'two'],
                    'd': [0, 1, 2, 0, 1, 2, 3]})
frame
abcd
007one0
116one1
225one2
334two0
443two1
552two2
661two3
frame2 = frame.set_index(['c','d'])
frame2
ab
cd
one007
116
225
two034
143
252
361
frame.set_index(['c','d'],drop=False)
abcd
cd
one007one0
116one1
225one2
two034two0
143two1
252two2
361two3
frame2.reset_index()
cdab
0one007
1one116
2one225
3two034
4two143
5two252
6two361
数据合并
left = pd.Dataframe({'key': ['K0', 'K1', 'K2', 'K3'],
                    'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3']})

right = pd.Dataframe({'key': ['K0', 'K1', 'K2', 'K3'],
                    'C': ['C0', 'C1', 'C2', 'C3'],
                    'D': ['D0', 'D1', 'D2', 'D3']})
left
ABkey
0A0B0K0
1A1B1K1
2A2B2K2
3A3B3K3
right
CDkey
0C0D0K0
1C1D1K1
2C2D2K2
3C3D3K3
pd.merge(left,right)
ABkeyCD
0A0B0K0C0D0
1A1B1K1C1D1
2A2B2K2C2D2
3A3B3K3C3D3
pd.merge(left,right,on='key')
ABkeyCD
0A0B0K0C0D0
1A1B1K1C1D1
2A2B2K2C2D2
3A3B3K3C3D3
#处理重复列名
df_obj1 = pd.Dataframe({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],
'data' : np.random.randint(0,10,7)})
df_obj2 = pd.Dataframe({'key': ['a', 'b', 'd'],
'data' : np.random.randint(0,10,3)})
df_obj1
datakey
01b
17b
21a
32c
48a
55a
60b
df_obj2
datakey
00a
14b
27d
print(pd.merge(df_obj1, df_obj2, on='key', suffixes=('_left', '_right')))
   data_left key  data_right
0          1   b           4
1          7   b           4
2          0   b           4
3          1   a           0
4          8   a           0
5          5   a           0
#按索引连接
df_obj1 = pd.Dataframe({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],
'data1' : np.random.randint(0,10,7)})
df_obj2 = pd.Dataframe({'data2' : np.random.randint(0,10,3)}, index=['a', 'b', 'd'])
df_obj1 
data1key
00b
17b
27a
32c
49a
57a
65b
df_obj2
data2
a1
b8
d4
print(pd.merge(df_obj1, df_obj2, left_on='key', right_index=True))
   data1 key  data2
0      0   b      8
1      7   b      8
6      5   b      8
2      7   a      1
4      9   a      1
5      7   a      1
print(pd.merge(df_obj1, df_obj2, left_on='key', right_index=True))
   data1 key  data2
0      0   b      8
1      7   b      8
6      5   b      8
2      7   a      1
4      9   a      1
5      7   a      1
join
left2 = pd.Dataframe([[1., 2.], [3., 4.], [5., 6.]],
                     index=['a', 'c', 'e'],
                     columns=['语文', '数学'])

right2 = pd.Dataframe([[7., 8.], [9., 10.], [11., 12.], [13, 14]],
                      index=['b', 'c', 'd', 'e'],
                      columns=['英语', '综合'])
left2
语文数学
a1.02.0
c3.04.0
e5.06.0
right2
英语综合
b7.08.0
c9.010.0
d11.012.0
e13.014.0
pd.merge(left2,right2,how='outer',left_index=True,right_index=True)
语文数学英语综合
a1.02.0NaNNaN
bNaNNaN7.08.0
c3.04.09.010.0
dNaNNaN11.012.0
e5.06.013.014.0
# 按照索引合并  但是要求没有重叠的列
left2.join(right2,how='outer')
语文数学英语综合
a1.02.0NaNNaN
bNaNNaN7.08.0
c3.04.09.010.0
dNaNNaN11.012.0
e5.06.013.014.0
 
pd.concat 
df1 = pd.Dataframe(np.arange(6).reshape(3,2),index=list('abc'),columns=['one','two'])

df2 = pd.Dataframe(np.arange(4).reshape(2,2)+5,index=list('ac'),columns=['one','two'])
df1
onetwo
a01
b23
c45
df2
onetwo
a56
c78
pd.concat([df1,df2])  #默认外连接
onetwo
a01
b23
c45
a56
c78
pd.concat([df1,df2],axis=1) 
onetwoonetwo
a015.06.0
b23NaNNaN
c457.08.0
重塑和轴向旋转 重塑层次化索引
data = pd.Dataframe(np.arange(6).reshape((2, 3)),
                    index=pd.Index(['老王', '小刘'], name='姓名'),
                    columns=pd.Index(['语文', '数学', '英语'],name='科目'))
data
科目语文数学英语
姓名
老王012
小刘345
r = data.stack()
r
姓名  科目
老王  语文    0
    数学    1
    英语    2
小刘  语文    3
    数学    4
    英语    5
dtype: int32
type(r)
pandas.core.series.Series
r.unstack()
科目语文数学英语
姓名
老王012
小刘345
r.unstack(0)
姓名老王小刘
科目
语文03
数学14
英语25
r.unstack('姓名')
姓名老王小刘
科目
语文03
数学14
英语25
a1 = pd.Series(np.arange(4),index=list('abcd'))
a1
a    0
b    1
c    2
d    3
dtype: int32
a2 = pd.Series([4,5,6],index=list('cde'))
a2
c    4
d    5
e    6
dtype: int64
s1 = pd.concat([a1,a2],keys=['data1','data2'])
s1
data1  a    0
       b    1
       c    2
       d    3
data2  c    4
       d    5
       e    6
dtype: int64
type(s1)
pandas.core.series.Series
s1.unstack()
abcde
data10.01.02.03.0NaN
data2NaNNaN4.05.06.0
# stack()默认过滤缺失数据
s1.unstack().stack()
data1  a    0.0
       b    1.0
       c    2.0
       d    3.0
data2  c    4.0
       d    5.0
       e    6.0
dtype: float64
s1.unstack().stack(dropna=False)
data1  a    0.0
       b    1.0
       c    2.0
       d    3.0
       e    NaN
data2  a    NaN
       b    NaN
       c    4.0
       d    5.0
       e    6.0
dtype: float64
轴向旋转
df3 = pd.Dataframe({'date':['2018-11-22','2018-11-22','2018-11-23','2018-11-23','2018-11-24'],
                  'class':['a','b','b','c','c'],
                  'values':[5,3,2,6,1]},columns=['date','class','values'])
df3
dateclassvalues
02018-11-22a5
12018-11-22b3
22018-11-23b2
32018-11-23c6
42018-11-24c1
df3.pivot('date','class','values')
classabc
date
2018-11-225.03.0NaN
2018-11-23NaN2.06.0
2018-11-24NaNNaN1.0
df3.pivot('date','class')
values
classabc
date
2018-11-225.03.0NaN
2018-11-23NaN2.06.0
2018-11-24NaNNaN1.0
df3.set_index(['date','class']).unstack('class')
values
classabc
date
2018-11-225.03.0NaN
2018-11-23NaN2.06.0
2018-11-24NaNNaN1.0
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/360576.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号