Fork me on GitHub

Pandas入门学习笔记

根据莫凡Python学习Pandas的笔记

In [ ]:
print('hello,python3-pandas')

Pandas基本介绍

Pandas主要数据类型:

  • Series
  • DataFrame
In [3]:
import pandas as pd
import numpy as np
# Series的字符串表现形式为:索引在左边,值在右边。
# 由于我们没有为数据指定索引。于是会自动创建一个0到N-1(N为长度)的整数型索引。
s = pd.Series([1,3,6,np.nan,44,1])

print(s)
0     1.0
1     3.0
2     6.0
3     NaN
4    44.0
5     1.0
dtype: float64
In [8]:
# DataFrame是一个表格型的数据结构,它包含有一组有序的列,每列可以是不同的值类型(数值,字符串,布尔值等)。
# DataFrame既有行索引也有列索引, 它可以被看做由Series组成的大字典。
dates = pd.date_range('20180514',periods=6)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])

print(df)
                   a         b         c         d
2018-05-14  1.144771  1.326512  0.255163  0.554244
2018-05-15  0.663218  0.303554 -0.883772 -0.783526
2018-05-16  0.704013 -0.749445  0.926410 -0.327929
2018-05-17  0.304832  1.985809 -2.093082 -0.268555
2018-05-18 -0.298463 -0.686511 -0.517345 -0.269273
2018-05-19 -0.063719  1.761023  0.685568  0.086374
In [9]:
# 索引数据表的列
print(df['b'])
2018-05-14    1.326512
2018-05-15    0.303554
2018-05-16   -0.749445
2018-05-17    1.985809
2018-05-18   -0.686511
2018-05-19    1.761023
Freq: D, Name: b, dtype: float64
In [24]:
# 根据dict生成DataFrame
df1  = pd.DataFrame({'A' : 1.,
                    'B' : pd.Timestamp('20130102'),
                    'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
                    'D' : np.array([3] * 4,dtype='int32'),
                    'E' : pd.Categorical(["test","train","test","train"]),
                    'F' : 'foo'})
                    
print(df1)
print('-'*20)
输出数据表的每一列数据类型
print(df1.dtypes)
print('-'*20)
输出数据表行索引
print(df1.index)
print('-'*20)
# 输出数据表列索引
print(df1.columns)
print('-'*20)
# 返回数据表的值
print(df1.values)
print('-'*20)
# 数据表基本描述
print(df1.describe())
print('-'*20)
# 数据表转置
print(df1.T)
print('-'*20)
# 对数据索引进行排序,axis=1为沿着横向
print(df2.sort_index(axis=1, ascending=False))
print('-'*20)
# 对数据值进行排序
print(df2.sort_values(by='E'))
     A          B    C  D      E    F
0  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
2  1.0 2013-01-02  1.0  3   test  foo
3  1.0 2013-01-02  1.0  3  train  foo
--------------------
A           float64
B    datetime64[ns]
C           float32
D             int32
E          category
F            object
dtype: object
--------------------
Int64Index([0, 1, 2, 3], dtype='int64')
--------------------
Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
--------------------
[[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
 [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']
 [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo']
 [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']]
--------------------
         A    C    D
count  4.0  4.0  4.0
mean   1.0  1.0  3.0
std    0.0  0.0  0.0
min    1.0  1.0  3.0
25%    1.0  1.0  3.0
50%    1.0  1.0  3.0
75%    1.0  1.0  3.0
max    1.0  1.0  3.0
--------------------
                     0                    1                    2  \
A                    1                    1                    1   
B  2013-01-02 00:00:00  2013-01-02 00:00:00  2013-01-02 00:00:00   
C                    1                    1                    1   
D                    3                    3                    3   
E                 test                train                 test   
F                  foo                  foo                  foo   

                     3  
A                    1  
B  2013-01-02 00:00:00  
C                    1  
D                    3  
E                train  
F                  foo  
--------------------
     F      E  D    C          B    A
0  foo   test  3  1.0 2013-01-02  1.0
1  foo  train  3  1.0 2013-01-02  1.0
2  foo   test  3  1.0 2013-01-02  1.0
3  foo  train  3  1.0 2013-01-02  1.0
--------------------
     A          B    C  D      E    F
0  1.0 2013-01-02  1.0  3   test  foo
2  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
3  1.0 2013-01-02  1.0  3  train  foo

Pandas选择数据

  • 类似列表一样选择/切片
  • 根据标签loc选择
  • iloc
In [26]:
dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)), index=dates, columns=['A','B','C','D'])
print(df)
             A   B   C   D
2013-01-01   0   1   2   3
2013-01-02   4   5   6   7
2013-01-03   8   9  10  11
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23
In [33]:
# 简单筛选数据
print(df.A)
print('-'*10,'another way','-'*10)
print(df['A'])
2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
Freq: D, Name: A, dtype: int64
---------- another way ----------
2013-01-01     0
2013-01-02     4
2013-01-03     8
2013-01-04    12
2013-01-05    16
2013-01-06    20
Freq: D, Name: A, dtype: int64
In [32]:
print(df[0:3])
print('-'*10,'another way','-'*10)
print(df['2013-01-01':'2013-01-03'])
            A  B   C   D
2013-01-01  0  1   2   3
2013-01-02  4  5   6   7
2013-01-03  8  9  10  11
---------- another way ----------
            A  B   C   D
2013-01-01  0  1   2   3
2013-01-02  4  5   6   7
2013-01-03  8  9  10  11
In [37]:
# 根据标签loc
print(df.loc['20130102'])
print('-'*20)
print(df.loc[:,['A','B']])
print('-'*20)
print(df.loc['20130102',['A','B']])
A    4
B    5
C    6
D    7
Name: 2013-01-02 00:00:00, dtype: int64
--------------------
             A   B
2013-01-01   0   1
2013-01-02   4   5
2013-01-03   8   9
2013-01-04  12  13
2013-01-05  16  17
2013-01-06  20  21
--------------------
A    4
B    5
Name: 2013-01-02 00:00:00, dtype: int64
In [41]:
# 根据序列iloc选择,item location?
print(df.iloc[3,1])
print('-'*20)
print(df.iloc[3:5,1:3])
print('-'*20)
print(df.iloc[[1,3,5], 1:3])
13
--------------------
             B   C
2013-01-04  13  14
2013-01-05  17  18
--------------------
             B   C
2013-01-02   5   6
2013-01-04  13  14
2013-01-06  21  22
In [43]:
# 通过判断进行筛选
print(df[df.A > 8])
             A   B   C   D
2013-01-04  12  13  14  15
2013-01-05  16  17  18  19
2013-01-06  20  21  22  23

pandas修改数据

  • iloc
  • loc
In [45]:
# 先选择后赋值
df.iloc[2,2] = 1111
df.loc['20130101','B'] = 2222
print(df)
             A     B     C   D
2013-01-01   0  2222     2   3
2013-01-02   4     5     6   7
2013-01-03   8     9  1111  11
2013-01-04  12    13    14  15
2013-01-05  16    17    18  19
2013-01-06  20    21    22  23
In [47]:
# 根据条件赋值
df.B[df.A>4] = 0
print(df)
             A     B     C   D
2013-01-01   0  2222     2   3
2013-01-02   4     5     6   7
2013-01-03   8     0  1111  11
2013-01-04  12     0    14  15
2013-01-05  16     0    18  19
2013-01-06  20     0    22  23
In [48]:
# 按行或列赋值(此处加上一列)
df['F'] = np.nan
print(df)
             A     B     C   D   F
2013-01-01   0  2222     2   3 NaN
2013-01-02   4     5     6   7 NaN
2013-01-03   8     0  1111  11 NaN
2013-01-04  12     0    14  15 NaN
2013-01-05  16     0    18  19 NaN
2013-01-06  20     0    22  23 NaN
In [49]:
# 使用Series来加上一列
df['E'] = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130101',periods=6)) 
print(df)
             A     B     C   D   F  E
2013-01-01   0  2222     2   3 NaN  1
2013-01-02   4     5     6   7 NaN  2
2013-01-03   8     0  1111  11 NaN  3
2013-01-04  12     0    14  15 NaN  4
2013-01-05  16     0    18  19 NaN  5
2013-01-06  20     0    22  23 NaN  6

pandas处理缺失数据

  • dropna() 直接抛弃有NAN的行/列
  • fillna() 将NAN数据位置用其他数据代替
  • isnull() 判断是否存在NAN数据位

以上函数均不改变原始数据,返回修改后的数据。

In [57]:
# 创建一个6*4矩阵,并将两个位置置空
dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D'])
df.iloc[0,1] = np.nan
df.iloc[1,2] = np.nan
print(df)
             A     B     C   D
2013-01-01   0   NaN   2.0   3
2013-01-02   4   5.0   NaN   7
2013-01-03   8   9.0  10.0  11
2013-01-04  12  13.0  14.0  15
2013-01-05  16  17.0  18.0  19
2013-01-06  20  21.0  22.0  23
In [63]:
# axis=0沿着纵向操作,axis=1沿着横向操作
# 'any'只要存在NAN就drop,'all'必须全为NAN才drop
# dropna()不改变原始数据,只返回修改后的数据
df1 = df.dropna(axis=0, how='any')
print('-'*10,'dropna result','-'*10)
print(df1)
print('-'*10,'original data','-'*10)
print(df)
---------- dropna result ----------
             A     B     C   D
2013-01-03   8   9.0  10.0  11
2013-01-04  12  13.0  14.0  15
2013-01-05  16  17.0  18.0  19
2013-01-06  20  21.0  22.0  23
---------- original data ----------
             A     B     C   D
2013-01-01   0   NaN   2.0   3
2013-01-02   4   5.0   NaN   7
2013-01-03   8   9.0  10.0  11
2013-01-04  12  13.0  14.0  15
2013-01-05  16  17.0  18.0  19
2013-01-06  20  21.0  22.0  23
In [65]:
df2 = df.fillna(value=0)
print('-'*10,'fillna result','-'*10)
print(df2)
---------- fillna result ----------
             A     B     C   D
2013-01-01   0   0.0   2.0   3
2013-01-02   4   5.0   0.0   7
2013-01-03   8   9.0  10.0  11
2013-01-04  12  13.0  14.0  15
2013-01-05  16  17.0  18.0  19
2013-01-06  20  21.0  22.0  23
In [66]:
df3 = df.isnull()
print(df3)
                A      B      C      D
2013-01-01  False   True  False  False
2013-01-02  False  False   True  False
2013-01-03  False  False  False  False
2013-01-04  False  False  False  False
2013-01-05  False  False  False  False
2013-01-06  False  False  False  False

pandas导入导出数据(略)

pandas可以读取与存取的资料格式有很多种,像csv、excel、json、html与pickle等…

pandas合并concat

几个关键参数:

  • axis 合并方向
    • axis=0 沿着纵向
    • axis=1 沿着横向
  • ignore_index 重置index
  • join 合并方式
    • outer 保留不交叉的数据
    • inner 抛弃不交叉的数据
  • join_axes 依照axes合并,为最终数据设定合并的一个Base
In [70]:
import pandas as pd
import numpy as np

# 创建数据集
df0 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'])
df1 = pd.DataFrame(np.ones((3,4))*1, columns=['a','b','c','d'])
df2 = pd.DataFrame(np.ones((3,4))*2, columns=['a','b','c','d'])

# concat纵向合并,axis=0为沿纵向
res = pd.concat([df0,df1,df2], axis=0)

print(res)
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
0  1.0  1.0  1.0  1.0
1  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0
0  2.0  2.0  2.0  2.0
1  2.0  2.0  2.0  2.0
2  2.0  2.0  2.0  2.0
In [72]:
# ignore_index来重置index
res = pd.concat([df0, df1, df2], axis=0, ignore_index=True)
print(res)
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
5  1.0  1.0  1.0  1.0
6  2.0  2.0  2.0  2.0
7  2.0  2.0  2.0  2.0
8  2.0  2.0  2.0  2.0
In [77]:
# join设置合并方式,缺省值为'outer'
# axis=0时是沿着纵向做纵向合并,有相同的列则上下合并在一起,其他独自的列自列,原本没有值的位置皆以NaN填充。

#定义资料集
df0 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'], index=[1,2,3])
df1 = pd.DataFrame(np.ones((3,4))*1, columns=['b','c','d','e'], index=[2,3,4])

# 沿纵向‘外’合并df0和df1
res = pd.concat([df0,df1], axis=0, join='outer')
print('-'*10,'axis=0沿着纵向','-'*10)
print(res)

# 沿横向‘外’合并df0和df1
res = pd.concat([df0,df1], axis=1, join='outer')
print('-'*10,'axis=1沿着横向','-'*10)
print(res)
---------- axis=0沿着纵向 ----------
     a    b    c    d    e
1  0.0  0.0  0.0  0.0  NaN
2  0.0  0.0  0.0  0.0  NaN
3  0.0  0.0  0.0  0.0  NaN
2  NaN  1.0  1.0  1.0  1.0
3  NaN  1.0  1.0  1.0  1.0
4  NaN  1.0  1.0  1.0  1.0
---------- axis=1沿着横向 ----------
     a    b    c    d    b    c    d    e
1  0.0  0.0  0.0  0.0  NaN  NaN  NaN  NaN
2  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0
3  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0
4  NaN  NaN  NaN  NaN  1.0  1.0  1.0  1.0
In [80]:
# 沿纵向‘内’合并df0和df1,并重置index
res = pd.concat([df0,df1], axis=0, join='inner', ignore_index=True)
print(res)
     b    c    d
0  0.0  0.0  0.0
1  0.0  0.0  0.0
2  0.0  0.0  0.0
3  1.0  1.0  1.0
4  1.0  1.0  1.0
5  1.0  1.0  1.0
In [81]:
#依照`df0.index`进行横向合并,最终只保留df0的index[1,2,3]
res = pd.concat([df0, df1], axis=1, join_axes=[df0.index])
print(res)
     a    b    c    d    b    c    d    e
1  0.0  0.0  0.0  0.0  NaN  NaN  NaN  NaN
2  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0
3  0.0  0.0  0.0  0.0  1.0  1.0  1.0  1.0

pandas.append()合并数据

append只能纵向合并,不能横向合并

In [91]:
#定义资料集
df0 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'])
df1 = pd.DataFrame(np.ones((3,4))*1, columns=['a','b','c','d'])
df2 = pd.DataFrame(np.ones((3,4))*2, columns=['a','b','c','d'])
s1 = pd.Series([1,2,3,4], index=['a','b','c','d'])

#将df2合并到df1的下面,以及重置index
res = df0.append(df1, ignore_index=True)

print('-'*6,'df0+df1', '-'*6)
print(res)
#合并多个df,将df2与df3合并至df1的下面,以及重置index
res = df0.append([df1,df2], ignore_index=True)

print('-'*6,'df0+df1+df2', '-'*6)
print(res)
#合并series,将s1合并至df0,以及重置index
res = df0.append(s1, ignore_index=True)

print('-'*6,'df0+Series', '-'*6)
print(res)
------ df0+df1 ------
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
5  1.0  1.0  1.0  1.0
------ df0+df1+df2 ------
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
5  1.0  1.0  1.0  1.0
6  2.0  2.0  2.0  2.0
7  2.0  2.0  2.0  2.0
8  2.0  2.0  2.0  2.0
------ df0+Series ------
     a    b    c    d
0  0.0  0.0  0.0  0.0
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  1.0  2.0  3.0  4.0

pandas合并merge

pandas中的merge和concat类似,但主要是用于两组有key column的数据,统一索引的数据. 通常也被用在Database的处理当中.
几个关键词:

  • on 合并基准
  • indicator 合并记录
  • how 合并方式
  • index 行索引
In [100]:
# 依据一组key合并
import pandas as pd

#定义资料集
left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
                     'A': ['A0', 'A1', 'A2', 'A3'],
                     'B': ['B0', 'B1', 'B2', 'B3']})
right = pd.DataFrame({'key': ['K0', 'K1', 'K4', 'K3'],
                      'C': ['C0', 'C1', 'C2', 'C3'],
                      'D': ['D0', 'D1', 'D2', 'D3']})
print('-'*8,'left','-'*8)
print(left)
print('-'*8,'right','-'*8)
print(right)
# 合并有公共key值的行,key为merge的基准
res = pd.merge(left, right, on='key')
print('-'*8,'merge result','-'*8)
print(res)
-------- left --------
    A   B key
0  A0  B0  K0
1  A1  B1  K1
2  A2  B2  K2
3  A3  B3  K3
-------- right --------
    C   D key
0  C0  D0  K0
1  C1  D1  K1
2  C2  D2  K4
3  C3  D3  K3
-------- merge result --------
    A   B key   C   D
0  A0  B0  K0  C0  D0
1  A1  B1  K1  C1  D1
2  A3  B3  K3  C3  D3
In [103]:
# 依据两组key进行合并
# 合并时有4种方法how = ['left', 'right', 'outer', 'inner'],缺省值how='inner'

#定义资料集
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']})
print('-'*8,'left','-'*8)
print(left)
print('-'*8,'right','-'*8)
print(right)

#依据key1与key2 columns进行合并,并打印出四种结果['left', 'right', 'outer', 'inner']

# innner将[key1,key2]组合相同的行 合并为一行,其余丢弃
res = pd.merge(left, right, on=['key1', 'key2'], how='inner')
print('-'*8,'inner merge result','-'*8)
print(res)
# innner将[key1,key2]组合相同的行 合并为一行,其余无法合并的行中空位用NAN补齐
res = pd.merge(left, right, on=['key1', 'key2'], how='outer')
print('-'*8,'outer merge result','-'*8)
print(res)
# 对outer模式进行限制,以左侧为基准j进行merge,右侧空位补NAN
res = pd.merge(left, right, on=['key1', 'key2'], how='left')
print('-'*8,'left merge result','-'*8)
print(res)

res = pd.merge(left, right, on=['key1', 'key2'], how='right')
print('-'*8,'right merge result','-'*8)
print(res)
-------- left --------
    A   B key1 key2
0  A0  B0   K0   K0
1  A1  B1   K0   K1
2  A2  B2   K1   K0
3  A3  B3   K2   K1
-------- right --------
    C   D key1 key2
0  C0  D0   K0   K0
1  C1  D1   K1   K0
2  C2  D2   K1   K0
3  C3  D3   K2   K0
-------- inner merge result --------
    A   B key1 key2   C   D
0  A0  B0   K0   K0  C0  D0
1  A2  B2   K1   K0  C1  D1
2  A2  B2   K1   K0  C2  D2
-------- outer merge result --------
     A    B key1 key2    C    D
0   A0   B0   K0   K0   C0   D0
1   A1   B1   K0   K1  NaN  NaN
2   A2   B2   K1   K0   C1   D1
3   A2   B2   K1   K0   C2   D2
4   A3   B3   K2   K1  NaN  NaN
5  NaN  NaN   K2   K0   C3   D3
-------- left merge result --------
    A   B key1 key2    C    D
0  A0  B0   K0   K0   C0   D0
1  A1  B1   K0   K1  NaN  NaN
2  A2  B2   K1   K0   C1   D1
3  A2  B2   K1   K0   C2   D2
4  A3  B3   K2   K1  NaN  NaN
-------- right merge result --------
     A    B key1 key2   C   D
0   A0   B0   K0   K0  C0  D0
1   A2   B2   K1   K0  C1  D1
2   A2   B2   K1   K0  C2  D2
3  NaN  NaN   K2   K0  C3  D3
In [104]:
# indicator = True会将每一行合并的方式保存在新的一列

df1 = pd.DataFrame({'col1':[0,1], 'col_left':['a','b']})
df2 = pd.DataFrame({'col1':[1,2,2],'col_right':[2,2,2]})

print(df1)
print(df2)

# 依据col1进行合并,并启用indicator=True,最后打印出
res = pd.merge(df1, df2, on='col1', how='outer', indicator=True)
print(res)

# 自定indicator column的名称,并打印出
res = pd.merge(df1, df2, on='col1', how='outer', indicator='indicator_column')
print(res)
   col1 col_left
0     0        a
1     1        b
   col1  col_right
0     1          2
1     2          2
2     2          2
   col1 col_left  col_right      _merge
0     0        a        NaN   left_only
1     1        b        2.0        both
2     2      NaN        2.0  right_only
3     2      NaN        2.0  right_only
   col1 col_left  col_right indicator_column
0     0        a        NaN        left_only
1     1        b        2.0             both
2     2      NaN        2.0       right_only
3     2      NaN        2.0       right_only
In [109]:
# 依据index进行合并
left = pd.DataFrame({'A': ['A0', 'A1', 'A2'],
                     'B': ['B0', 'B1', 'B2']},
                     index=['K0', 'K1', 'K2'])
right = pd.DataFrame({'C': ['C0', 'C2', 'C3'],
                      'D': ['D0', 'D2', 'D3']},
                     index=['K0', 'K2', 'K3'])
print(left)
print(right)
#依据左右资料集的index进行合并,how='outer'
res = pd.merge(left, right, left_index=True, right_index=True, how='outer')
print(res)
     A   B
K0  A0  B0
K1  A1  B1
K2  A2  B2
     C   D
K0  C0  D0
K2  C2  D2
K3  C3  D3
      A    B    C    D
K0   A0   B0   C0   D0
K1   A1   B1  NaN  NaN
K2   A2   B2   C2   D2
K3  NaN  NaN   C3   D3
In [111]:
# 解决重名overlapping问题
#定义资料集
boys = pd.DataFrame({'k': ['K0', 'K1', 'K2'], 'age': [1, 2, 3]})
girls = pd.DataFrame({'k': ['K0', 'K0', 'K3'], 'age': [4, 5, 6]})

#使用suffixes解决overlapping的问题
res = pd.merge(boys, girls, on='k', suffixes=['_boy', '_girl'], how='inner')
print(res)
   age_boy   k  age_girl
0        1  K0         4
1        1  K0         5

pandas plot出图

  • Series可视化
  • DataFrame可视化
In [117]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 随机生成50个数据
data = pd.Series(np.random.randn(50),index=np.arange(50))
 
# 为了方便观看效果, 我们累加这个数据
data.cumsum()

# pandas 数据可以直接观看其可视化形式
data.plot()

plt.show()
In [123]:
# DataFrame可视化
data = pd.DataFrame(
    np.random.randn(20,4),
    index=np.arange(20),
    columns=list("ABCD")
    )
data.cumsum()
data.plot()
plt.show()
In [127]:
# 绘制散点图Scatter
ax = data.plot.scatter(x='A',y='B',color='DarkBlue',label='Class1')
# 下面data 画在上一个 ax 上面,一张figure上绘制两个散点图
data.plot.scatter(x='A',y='C',color='DarkRed',label='Class2',ax=ax)
plt.show()

The END

Comments