欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python常见的pandas用法demo示例

程序员文章站 2023-02-15 16:03:26
本文实例总结了python常见的pandas用法。分享给大家供大家参考,具体如下: import numpy as np import pandas as pd...

本文实例总结了python常见的pandas用法。分享给大家供大家参考,具体如下:

import numpy as np
import pandas as pd

s = pd.series([1,3,6, np.nan, 44, 1]) #定义一个序列。 序列就是一列内容,每一行有一个index值
print(s)
print(s.index)

0     1.0
1     3.0
2     6.0
3     nan
4    44.0
5     1.0
dtype: float64
rangeindex(start=0, stop=6, step=1)

dates = pd.date_range('20180101', periods=6)
print(dates)

datetimeindex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04',
               '2018-01-05', '2018-01-06'],
              dtype='datetime64[ns]', freq='d')

df1 = pd.dataframe(np.arange(12).reshape(3,4)) #定义dataframe,可以看作一个有index和colunms的矩阵
print(df)

   0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11

df2 = pd.dataframe(np.random.randn(6,4), index=dates, columns=['a', 'b', 'c', 'd']) #np.random.randn(6,4)生成6行4列矩阵
print(df)

                   a         b         c         d
2018-01-01  0.300675  1.769383  1.244406 -1.058294
2018-01-02  0.832666  2.216755  0.178716 -0.156828
2018-01-03  1.314190 -0.866199  0.836150  1.001026
2018-01-04 -1.671724  1.147406 -0.148676 -0.272555
2018-01-05  1.146664  2.022861 -1.833995 -0.627568
2018-01-06 -0.192242  1.517676  0.756707  0.058869

df = pd.dataframe({'a':1.0,
          'b':pd.timestamp('20180101'),
          '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'}) #按照给出的逐列定义df

print(df)
print(df.dtypes)

     a          b    c  d      e    f
0  1.0 2018-01-01  1.0  3   test  foo
1  1.0 2018-01-01  1.0  3  train  foo
2  1.0 2018-01-01  1.0  3   test  foo
3  1.0 2018-01-01  1.0  3  train  foo
a           float64
b    datetime64[ns]
c           float32
d             int32
e          category
f            object
dtype: object

#df的行、列、值
print(df.index)
print(df.columns)
print(df.values)

int64index([0, 1, 2, 3], dtype='int64')
index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')
[[1.0 timestamp('2018-01-01 00:00:00') 1.0 3 'test' 'foo']
 [1.0 timestamp('2018-01-01 00:00:00') 1.0 3 'train' 'foo']
 [1.0 timestamp('2018-01-01 00:00:00') 1.0 3 'test' 'foo']
 [1.0 timestamp('2018-01-01 00:00:00') 1.0 3 'train' 'foo']]

print(df.describe()) #统计
print(df.t) #转置

         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  2018-01-01 00:00:00  2018-01-01 00:00:00  2018-01-01 00:00:00
c                    1                    1                    1
d                    3                    3                    3
e                 test                train                 test
f                  foo                  foo                  foo
                     3
a                    1
b  2018-01-01 00:00:00
c                    1
d                    3
e                train
f                  foo

#df排序
print(df.sort_index(axis=1, ascending=false)) #根据索引值对各行进行排序(相当于重新排列各列的位置)
print(df.sort_values(by='e')) #根据内容值对各列进行排序

     f      e  d    c          b    a
0  foo   test  3  1.0 2018-01-01  1.0
1  foo  train  3  1.0 2018-01-01  1.0
2  foo   test  3  1.0 2018-01-01  1.0
3  foo  train  3  1.0 2018-01-01  1.0
     a          b    c  d      e    f
0  1.0 2018-01-01  1.0  3   test  foo
2  1.0 2018-01-01  1.0  3   test  foo
1  1.0 2018-01-01  1.0  3  train  foo
3  1.0 2018-01-01  1.0  3  train  foo

indexes = pd.date_range('20180101', periods=6)
df3 = pd.dataframe(np.arange(24).reshape(6, 4), index=indexes, columns=['a', 'b', 'c', 'd'])
print(df3)
print()
#选择column
print(df3['a'])
print()
print(df3.a)

             a   b   c   d
2018-01-01   0   1   2   3
2018-01-02   4   5   6   7
2018-01-03   8   9  10  11
2018-01-04  12  13  14  15
2018-01-05  16  17  18  19
2018-01-06  20  21  22  23
2018-01-01     0
2018-01-02     4
2018-01-03     8
2018-01-04    12
2018-01-05    16
2018-01-06    20
freq: d, name: a, dtype: int32
2018-01-01     0
2018-01-02     4
2018-01-03     8
2018-01-04    12
2018-01-05    16
2018-01-06    20
freq: d, name: a, dtype: int32
            a  b   c   d
2018-01-01  0  1   2   3
2018-01-02  4  5   6   7
2018-01-03  8  9  10  11

#选择行, 类似limit语句
print(df3[0:0])
print()
print(df3[0:3])
print()
print(df3['20180103':'20180105'])

empty dataframe
columns: [a, b, c, d]
index: []
            a  b   c   d
2018-01-01  0  1   2   3
2018-01-02  4  5   6   7
2018-01-03  8  9  10  11
             a   b   c   d
2018-01-03   8   9  10  11
2018-01-04  12  13  14  15
2018-01-05  16  17  18  19

print(df3.loc['20180102']) #返回指定行构成的序列

a    4
b    5
c    6
d    7
name: 2018-01-02 00:00:00, dtype: int32

print(df3.loc['20180103', ['a','c']]) #列筛选
print()
print(df3.loc['20180103':'20180105', ['a','c']]) #子df,类似select a, c from df limit ...
print()
print(df3.loc[:, ['a', 'b']])

a     8
c    10
name: 2018-01-03 00:00:00, dtype: int32
             a   c
2018-01-03   8  10
2018-01-04  12  14
2018-01-05  16  18
             a   b
2018-01-01   0   1
2018-01-02   4   5
2018-01-03   8   9
2018-01-04  12  13
2018-01-05  16  17
2018-01-06  20  21

print(df3);print()
print(df3.iloc[1]);print()
print(df3.iloc[1,1]);print()
print(df3.iloc[:,1]);print()
print(df3.iloc[0:3,1:3]);print()
print(df3.iloc[[1,3,5],[0,2]]) #行可以不连续,limit做不到

             a   b   c   d
2018-01-01   0   1   2   3
2018-01-02   4   5   6   7
2018-01-03   8   9  10  11
2018-01-04  12  13  14  15
2018-01-05  16  17  18  19
2018-01-06  20  21  22  23
a    4
b    5
c    6
d    7
name: 2018-01-02 00:00:00, dtype: int32
5
2018-01-01     1
2018-01-02     5
2018-01-03     9
2018-01-04    13
2018-01-05    17
2018-01-06    21
freq: d, name: b, dtype: int32
            b   c
2018-01-01  1   2
2018-01-02  5   6
2018-01-03  9  10
             a   c
2018-01-02   4   6
2018-01-04  12  14
2018-01-06  20  22

# print(df3.ix[:3, ['a', 'c']])\
print(df3);print()
print(df3[df3.a >= 8]) #根据值进行条件过滤,类似where a >= 8条件语句

             a   b   c   d
2018-01-01   0   1   2   3
2018-01-02   4   5   6   7
2018-01-03   8   9  10  11
2018-01-04  12  13  14  15
2018-01-05  16  17  18  19
2018-01-06  20  21  22  23
             a   b   c   d
2018-01-03   8   9  10  11
2018-01-04  12  13  14  15
2018-01-05  16  17  18  19
2018-01-06  20  21  22  23

indexes1 = pd.date_range('20180101', periods=6)
df4 = pd.dataframe(np.arange(24).reshape(6, 4), index=indexes1, columns=['a', 'b', 'c', 'd'])
print(df4);print()
#给某个元素赋值
df4.a[1] = 1111
df4.b['20180103'] = 2222
df4.iloc[3, 2] = 3333
df4.loc['20180105', 'd'] = 4444
print(df4);print()
#范围赋值
df4.b[df4.a < 10] = -1
print(df4);print()
df4[df4.a < 10] = 0
print(df4);print()

             a   b   c   d
2018-01-01   0   1   2   3
2018-01-02   4   5   6   7
2018-01-03   8   9  10  11
2018-01-04  12  13  14  15
2018-01-05  16  17  18  19
2018-01-06  20  21  22  23
               a     b     c     d
2018-01-01     0     1     2     3
2018-01-02  1111     5     6     7
2018-01-03     8  2222    10    11
2018-01-04    12    13  3333    15
2018-01-05    16    17    18  4444
2018-01-06    20    21    22    23
               a   b     c     d
2018-01-01     0  -1     2     3
2018-01-02  1111   5     6     7
2018-01-03     8  -1    10    11
2018-01-04    12  13  3333    15
2018-01-05    16  17    18  4444
2018-01-06    20  21    22    23
               a   b     c     d
2018-01-01     0   0     0     0
2018-01-02  1111   5     6     7
2018-01-03     0   0     0     0
2018-01-04    12  13  3333    15
2018-01-05    16  17    18  4444
2018-01-06    20  21    22    23

indexes1 = pd.date_range('20180101', periods=6)
df4 = pd.dataframe(np.arange(24).reshape(6, 4), index=indexes1, columns=['a', 'b', 'c', 'd'])
print(df4);print()
#添加一列
df4['e'] = np.nan
print(df4);print()
#由于index没对齐,原df没有的行默认为nan,类型为float64,多出的行丢弃
df4['f'] = pd.series([1,2,3,4,5,6], index=pd.date_range('20180102', periods=6))
print(df4);print()
print(df4.dtypes)

             a   b   c   d
2018-01-01   0   1   2   3
2018-01-02   4   5   6   7
2018-01-03   8   9  10  11
2018-01-04  12  13  14  15
2018-01-05  16  17  18  19
2018-01-06  20  21  22  23
             a   b   c   d   e
2018-01-01   0   1   2   3 nan
2018-01-02   4   5   6   7 nan
2018-01-03   8   9  10  11 nan
2018-01-04  12  13  14  15 nan
2018-01-05  16  17  18  19 nan
2018-01-06  20  21  22  23 nan
             a   b   c   d   e    f
2018-01-01   0   1   2   3 nan  nan
2018-01-02   4   5   6   7 nan  1.0
2018-01-03   8   9  10  11 nan  2.0
2018-01-04  12  13  14  15 nan  3.0
2018-01-05  16  17  18  19 nan  4.0
2018-01-06  20  21  22  23 nan  5.0
a      int32
b      int32
c      int32
d      int32
e    float64
f    float64
dtype: object

df_t = pd.dataframe(np.arange(24).reshape(6, 4), index=[1,2,3,4,5,6], columns=['a', 'b', 'c', 'd'])
df_t.iloc[0, 1] = np.nan
df_t.iloc[1, 2] = np.nan
df = df_t.copy()
print(df);print()
print(df.dropna(axis=0, how='any'));print()
df = df_t.copy()
print(df.dropna(axis=1, how='any'));print()
df = df_t.copy()
df.c = np.nan
print(df);print()
print(df.dropna(axis=1, how='all'));print()

    a     b     c   d
1   0   nan   2.0   3
2   4   5.0   nan   7
3   8   9.0  10.0  11
4  12  13.0  14.0  15
5  16  17.0  18.0  19
6  20  21.0  22.0  23
    a     b     c   d
3   8   9.0  10.0  11
4  12  13.0  14.0  15
5  16  17.0  18.0  19
6  20  21.0  22.0  23
    a   d
1   0   3
2   4   7
3   8  11
4  12  15
5  16  19
6  20  23
    a     b   c   d
1   0   nan nan   3
2   4   5.0 nan   7
3   8   9.0 nan  11
4  12  13.0 nan  15
5  16  17.0 nan  19
6  20  21.0 nan  23
    a     b   d
1   0   nan   3
2   4   5.0   7
3   8   9.0  11
4  12  13.0  15
5  16  17.0  19
6  20  21.0  23

df = df_t.copy()
print(df);print()
print(df.isna());print()
print(df.isnull().any());print() #isnull是isna别名,功能一样
print(df.isnull().any(axis=1));print()
print(np.any(df.isna() == true));print()
print(df.fillna(value=0)) #将nan赋值

    a     b     c   d
1   0   nan   2.0   3
2   4   5.0   nan   7
3   8   9.0  10.0  11
4  12  13.0  14.0  15
5  16  17.0  18.0  19
6  20  21.0  22.0  23
       a      b      c      d
1  false   true  false  false
2  false  false   true  false
3  false  false  false  false
4  false  false  false  false
5  false  false  false  false
6  false  false  false  false
a    false
b     true
c     true
d    false
dtype: bool
1     true
2     true
3    false
4    false
5    false
6    false
dtype: bool
true
    a     b     c   d
1   0   0.0   2.0   3
2   4   5.0   0.0   7
3   8   9.0  10.0  11
4  12  13.0  14.0  15
5  16  17.0  18.0  19
6  20  21.0  22.0  23

data = pd.read_csv('d:/pythonwp/test/student.csv')
print(data)
data.to_pickle('d:/pythonwp/test/student.pickle')

   id     name  age  gender
0   1       牛帅   23    male
1   2      gyb   89    male
2   3      xxs   27    male
3   4      hey   24  female
4   5    奥莱利赫本   66  female
5   6  jackson   61    male
6   7       牛帅   23    male

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'])
print(df0); print()
print(df1); print()
print(df2); print()
res = pd.concat([df0, df1, df2], axis = 0)
print(res); print()
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
     a    b    c    d
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
     a    b    c    d
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
     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
     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 = pd.dataframe(np.ones((3, 4)) * 0, columns=['a', 'b', 'c', 'd'])
df1 = pd.dataframe(np.ones((3, 4)) * 1, columns=['e', 'f', 'c', 'd'])
res = pd.concat([df0, df1], ignore_index=true)
print(res);print()
res = pd.concat([df0, df1], join='outer', ignore_index=true)
print(res);print()
res = pd.concat([df0, df1], join='inner',ignore_index=true)
print(res);print()

     a    b    c    d    e    f
0  0.0  0.0  0.0  0.0  nan  nan
1  0.0  0.0  0.0  0.0  nan  nan
2  0.0  0.0  0.0  0.0  nan  nan
3  nan  nan  1.0  1.0  1.0  1.0
4  nan  nan  1.0  1.0  1.0  1.0
5  nan  nan  1.0  1.0  1.0  1.0
     a    b    c    d    e    f
0  0.0  0.0  0.0  0.0  nan  nan
1  0.0  0.0  0.0  0.0  nan  nan
2  0.0  0.0  0.0  0.0  nan  nan
3  nan  nan  1.0  1.0  1.0  1.0
4  nan  nan  1.0  1.0  1.0  1.0
5  nan  nan  1.0  1.0  1.0  1.0
     c    d
0  0.0  0.0
1  0.0  0.0
2  0.0  0.0
3  1.0  1.0
4  1.0  1.0
5  1.0  1.0

#横向合并
df0 = pd.dataframe(np.ones((3, 4)) * 0, index=['1', '2', '3'], columns=['a', 'b', 'c', 'd'])
df1 = pd.dataframe(np.ones((3, 4)) * 1, index=['2', '3', '4'], columns=['a', 'b', 'c', 'd'])
print(df0);print()
print(df1);print()
res = pd.concat([df0, df1], axis=1)
print(res);print()
res = pd.concat([df0, df1], axis=1, join='inner', ignore_index=true)
print(res);print()
res = pd.concat([df0, df1], axis=1, join_axes=[df0.index])
print(res);print()

     a    b    c    d
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0
     a    b    c    d
2  1.0  1.0  1.0  1.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
     a    b    c    d    a    b    c    d
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
     0    1    2    3    4    5    6    7
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
     a    b    c    d    a    b    c    d
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

df0 = pd.dataframe(np.ones((3, 4)) * 0, index=['1', '2', '3'], columns=['a', 'b', 'c', 'd'])
df1 = pd.dataframe(np.ones((3, 4)) * 1, index=['2', '3', '4'], columns=['a', 'b', 'c', 'd'])
print(df0);print()
print(df1);print()
res = df0.append([df1, df1], ignore_index=false)
print(res);print()
s = pd.series([1,2,3,4], index=['a','b','c','e'])
print(df0.append(s, ignore_index=true))

     a    b    c    d
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0
     a    b    c    d
2  1.0  1.0  1.0  1.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
     a    b    c    d
1  0.0  0.0  0.0  0.0
2  0.0  0.0  0.0  0.0
3  0.0  0.0  0.0  0.0
2  1.0  1.0  1.0  1.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
2  1.0  1.0  1.0  1.0
3  1.0  1.0  1.0  1.0
4  1.0  1.0  1.0  1.0
     a    b    c    d    e
0  0.0  0.0  0.0  0.0  nan
1  0.0  0.0  0.0  0.0  nan
2  0.0  0.0  0.0  0.0  nan
3  1.0  2.0  3.0  nan  4.0

df1 = pd.dataframe({'key':['k0', 'k1', 'k2'],
          'a':['a0', 'a1', 'a2'],
          'b':['b0', 'b1', 'b2']})
df2 = pd.dataframe({'key':['k3', 'k1', 'k2'],
          'c':['c3', 'c1', 'c2'],
          'd':['d3', 'd1', 'd2']})
print(df1); print()
print(df2); print()
res = pd.merge(df1, df2, on='key')
print(res); print()
res = pd.merge(df1, df2, on='key', how='outer')
print(res); print()
res = pd.merge(df1, df2, on='key', how='left')
print(res); print()
res = pd.merge(df1, df2, on='key', how='right')
print(res); print()

    a   b key
0  a0  b0  k0
1  a1  b1  k1
2  a2  b2  k2
    c   d key
0  c3  d3  k3
1  c1  d1  k1
2  c2  d2  k2
    a   b key   c   d
0  a1  b1  k1  c1  d1
1  a2  b2  k2  c2  d2
     a    b key    c    d
0   a0   b0  k0  nan  nan
1   a1   b1  k1   c1   d1
2   a2   b2  k2   c2   d2
3  nan  nan  k3   c3   d3
    a   b key    c    d
0  a0  b0  k0  nan  nan
1  a1  b1  k1   c1   d1
2  a2  b2  k2   c2   d2
     a    b key   c   d
0   a1   b1  k1  c1  d1
1   a2   b2  k2  c2  d2
2  nan  nan  k3  c3  d3

df1 = pd.dataframe({'key1':['k0', 'k0', 'k1'],
          'key2':['k0', 'k1', 'k1'],
          'a':['a0', 'a1', 'a2'],
          'b':['b0', 'b1', 'b2']})
df2 = pd.dataframe({'key1':['k0', 'k0', 'k1', 'k2'],
          'key2':['k0', 'k0', 'k1', 'k2'],
          'c':['c3', 'c1', 'c2', 'c4'],
          'd':['d3', 'd1', 'd2', 'd4']})
print(df1); print()
print(df2); print()
res = pd.merge(df1, df2, on=['key1','key2'])
print(res); print()
res = pd.merge(df1, df2, on=['key1','key2'], how='outer', indicator='indi')
print(res); print()

    a   b key1 key2
0  a0  b0   k0   k0
1  a1  b1   k0   k1
2  a2  b2   k1   k1
    c   d key1 key2
0  c3  d3   k0   k0
1  c1  d1   k0   k0
2  c2  d2   k1   k1
3  c4  d4   k2   k2
    a   b key1 key2   c   d
0  a0  b0   k0   k0  c3  d3
1  a0  b0   k0   k0  c1  d1
2  a2  b2   k1   k1  c2  d2
     a    b key1 key2    c    d        indi
0   a0   b0   k0   k0   c3   d3        both
1   a0   b0   k0   k0   c1   d1        both
2   a1   b1   k0   k1  nan  nan   left_only
3   a2   b2   k1   k1   c2   d2        both
4  nan  nan   k2   k2   c4   d4  right_only

#以上是根据值合并。下面根据index合并
df1 = pd.dataframe({'a':['a0', 'a1', 'a2'],
          'b':['b0', 'b1', 'b2']},
          index=['index0', 'index1', 'index2'])
df2 = pd.dataframe({'a':['c3', 'c1', 'c2'],
          'd':['d3', 'd1', 'd2']},
          index=['index3', 'index1', 'index2'])
print(df1); print()
print(df2); print()
res = pd.merge(df1, df2, left_index=true, right_index=true)
print(res); print()
res = pd.merge(df1, df2, left_index=true, right_index=true, how='outer', suffixes=['_b', '_g'])
print(res); print()

         a   b
index0  a0  b0
index1  a1  b1
index2  a2  b2
         a   d
index3  c3  d3
index1  c1  d1
index2  c2  d2
       a_x   b a_y   d
index1  a1  b1  c1  d1
index2  a2  b2  c2  d2
        a_b    b  a_g    d
index0   a0   b0  nan  nan
index1   a1   b1   c1   d1
index2   a2   b2   c2   d2
index3  nan  nan   c3   d3

res = df1.join(df2, how='outer', lsuffix='_left', rsuffix='_right') #不用on默认用索引合并
print(res);print()
res = df1.join(df2, on='b', how='outer', lsuffix='_left', rsuffix='_right') #用on指定df1的某列和df2的索引合并
print(res);print()

       a_left    b a_right    d
index0     a0   b0     nan  nan
index1     a1   b1      c1   d1
index2     a2   b2      c2   d2
index3    nan  nan      c3   d3
       a_left       b a_right    d
index0     a0      b0     nan  nan
index1     a1      b1     nan  nan
index2     a2      b2     nan  nan
index2    nan  index3      c3   d3
index2    nan  index1      c1   d1
index2    nan  index2      c2   d2

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt #画图模块
s = pd.series(np.random.randn(1000), index=np.arange(1000))
s = s.cumsum()
#须在命令行执行, jupyter会报错
#s.plot()
#plt.show()
df = pd.dataframe(np.random.randn(1000, 3), columns=['a', 'b', 'c'])
df = df.cumsum()
print(df.head()); print() #head默认显示前5行
#须在命令行执行, jupyter会报错
#s.plot()
#plt.show()
#须在命令行执行, jupyter会报错
#'bar', 'hist', 'box', 'kde', 'area', 'scatter', 'hexbin', 'pie'...
#class_b = df.plot.scatter(x='a', y='b', color='darkblue', label='class b') #画图,scatter<散点图>
#df.plot.scatter(x='a', y='c', color='darkred', label='class c', class_b=class_b)
#plt.show()

          a         b         c
0 -0.399363 -1.004210  0.641141
1 -1.970009 -0.608482 -0.758504
2 -3.081640 -0.617352 -1.143872
3 -2.174627 -1.383785 -1.011411
4 -1.415515 -1.892226 -2.511739

更多关于python相关内容感兴趣的读者可查看本站专题:《python操作excel表格技巧总结》、《python文件与目录操作技巧汇总》、《python文本文件操作技巧汇总》、《python数据结构与算法教程》、《python函数使用技巧总结》、《python字符串操作技巧汇总》及《python入门与进阶经典教程

希望本文所述对大家python程序设计有所帮助。