【Python数据分析】pandas索引介绍

一、pandas的索引

1、索引引入

import pandas as pd
import numpy as np
df = pd.DataFrame({'语文':[87,79,67,92],
                   '数学':[93,89,80,77],
                   '英语':[90,80,70,75]},
                  index=['张三', '李四', '王五', '赵六'])
se = pd.Series(np.arange(6),index= [chr(i) for i in range(65,71)])
print(df.index)   #获取Dataframe的行索引
print(df.columns)  #获取Dataframe的列索引
print(se.index)

运行结果:

Index(['张三', '李四', '王五', '赵六'], dtype='object')
Index(['语文', '数学', '英语'], dtype='object')
Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')

2.索引的方法

"""
1.判断索引是否属于 index 对象. in
2.连接两个 index 对象.append
3.判断索引是否存在.isin
4.删除索引.delete
5.判断索引对象是否存在重复值.is_unique
6.筛选出 index 对象去重后的元素.unique
7.插入元素.insert
8.判断递增或递减
is_monotonic  /  is_monotonic_increasing,is_monotonic_decreasing
9.计算交、并、差集
"""

import pandas as pd
df = pd.DataFrame({'语文':[87,79,67,92],
                   '数学':[93,89,80,77],
                   '英语':[90,80,70,75]},
                  index=['张三', '李四', '王五', '赵六'])
df1 = pd.DataFrame({'语文':[87,79,67,92,56],
                   '数学':[97,86,80,77,88],
                   'python':[67,80,64,75,67]},
                  index=['张三', '王四', '五一', '吴玉','王五'])
#查询‘ 杨或’在不在索引中
print("\n")
print('查询 ‘杨过’ 在不在索引中'.center(30,'='))
print('杨过' in df.index,'杨过' in df1.index)

#连接两个索引对象,生成一个新的索引对象
new_index = df.index.append(df1.index)
print('连接两个索引对象,生成一个新的索引对象'.center(30,'='))
print(new_index)

#判断一个索引是否在另一个索引存在  .isin,返回bool值 
aa = df.index.isin( df1.index )
print(aa)
aa = df1.index.isin( df.index )
print(aa)
#判断索引对象是否存在重复值.is_unique,含重复值返回False,否则返回True
print(new_index.is_unique,df.index.is_unique)
#筛选索引的对象,去重。
new_index_unique = new_index.unique()
print(new_index_unique)

运行结果:

========查询 ‘杨过’ 在不在索引中========
False False
=====连接两个索引对象,生成一个新的索引对象======
Index(['张三', '李四', '王五', '赵六', '张三', '王四', '五一', '吴玉', '王五'], dtype='object')
[ True False  True False]
[ True False False False  True]
False True
Index(['张三', '李四', '王五', '赵六', '王四', '五一', '吴玉'], dtype='object')

二、重新索引

1、对Series对象重新索引

#reindex:创建一个新索引对象
"""
**(1).如果新添加的索引没有对应的值,则默认为NaN。如果减少索引,就相当于一个切片操作**
**(2).参数 fill_value,可指定不存在索引的值**
**(3).参数method,重新索引时选择插值处理方式:**
**method='ffill'或'pad' 前向填充,method='bfill'或'backfill' 后向填充**
"""
import numpy as np
from pandas import DataFrame,Series

se1 = Series([2.1, 4.3, -1.6], index = ['c', 'e', 'h'])
print ("\n")
print (se1)
print ('重新指定索引及顺序,新增索引相应值为NaN'.center(40,'='))
ss = se1.reindex(['a', 'b', 'c', 'd', 'e','h']) #新增索引名a,b,d
print (ss)  
print ('重新指定索引及顺序,新增索引相应值为指定为0,fill_value=0'.center(40,'='))
print (se1.reindex(['a', 'b', 'c', 'd', 'e','h'], fill_value =0))  
print ('重新指定索引及顺序,新增索引按后向填充'.center(40,'='))
print (se1.reindex(['a', 'b', 'c', 'd', 'e','h'], method='bfill')) 
print ('重新指定索引及顺序,新增索引按前向填充'.center(40,'='))
print (se1.reindex(['a', 'b', 'c', 'd', 'e','h'], method='ffill'))  

运行结果:

c    2.1
e    4.3
h   -1.6
dtype: float64
=========重新指定索引及顺序,新增索引相应值为NaN==========
a    NaN
b    NaN
c    2.1
d    NaN
e    4.3
h   -1.6
dtype: float64
==重新指定索引及顺序,新增索引相应值为指定为0,fill_value=0===
a    0.0
b    0.0
c    2.1
d    0.0
e    4.3
h   -1.6
dtype: float64
==========重新指定索引及顺序,新增索引按后向填充===========
a    2.1
b    2.1
c    2.1
d    4.3
e    4.3
h   -1.6
dtype: float64
==========重新指定索引及顺序,新增索引按前向填充===========
a    NaN
b    NaN
c    2.1
d    2.1
e    4.3
h   -1.6
dtype: float64

2、对Dataframe对象重新索引,参数的用法相同

num =np.random.randint(1,999,size=12)
print ("\n")
print (num)
#建立一个三行四列的索引,index为索引名,columns为列名。
df1 = DataFrame(num.reshape(3, 4),
                  index = ['a', 'c', 'd'],
                  columns = ['item1', 'item2', 'item3','item4'])
print (df1)
print ('重新指定索引及顺序,新增索引相应值为NaN'.center(40,'='))
df2 = df1.reindex(['a', 'b', 'c', 'd'])
print (df2)
print ('重新指定索引及顺序,新增索引相应值为指定为0,fill_value=0'.center(40,'='))
df3 = df1.reindex(['a', 'b', 'c', 'd'],fill_value = 0)
print (df3)
print ('重新指定column及顺序,新增索引相应值为NaN'.center(40,'='))
states = [ 'item3','col2', 'item2','col1','item1']
print (df1.reindex(columns = states))
print ('对DataFrame重新指定索引并指定元素填充(默认对行索引填充)'.center(40,'='))
print (df1.reindex(index = ['a', 'b', 'c', 'd'],columns = states, method = 'ffill'))
#测试!
df4 = ['c','e','b','d','a','g','f']
print (df1.reindex(index = df4))

运行结果:

[455 516 817 904 486  37 963 332 358 306 897 811]
   item1  item2  item3  item4
a    455    516    817    904
c    486     37    963    332
d    358    306    897    811
=========重新指定索引及顺序,新增索引相应值为NaN==========
   item1  item2  item3  item4
a  455.0  516.0  817.0  904.0
b    NaN    NaN    NaN    NaN
c  486.0   37.0  963.0  332.0
d  358.0  306.0  897.0  811.0
==重新指定索引及顺序,新增索引相应值为指定为0,fill_value=0===
   item1  item2  item3  item4
a    455    516    817    904
b      0      0      0      0
c    486     37    963    332
d    358    306    897    811
=======重新指定column及顺序,新增索引相应值为NaN========
   item3  col2  item2  col1  item1
a    817   NaN    516   NaN    455
c    963   NaN     37   NaN    486
d    897   NaN    306   NaN    358
===对DataFrame重新指定索引并指定元素填充(默认对行索引填充)====
   item3  col2  item2  col1  item1
a    817   NaN    516   NaN    455
b    817   NaN    516   NaN    455
c    963   NaN     37   NaN    486
d    897   NaN    306   NaN    358
   item1  item2  item3  item4
c  486.0   37.0  963.0  332.0
e    NaN    NaN    NaN    NaN
b    NaN    NaN    NaN    NaN
d  358.0  306.0  897.0  811.0
a  455.0  516.0  817.0  904.0
g    NaN    NaN    NaN    NaN
f    NaN    NaN    NaN    NaN

三、pandas的多层索引

1.创建多层行索引

"""
隐式构造,对index参数传递两个或更多的数组,Series也可以创建多层索引。"""
import pandas as pd
import numpy as np
dd = pd.Series(np.random.randint(1,100,size=6),index=list('abcdef'))
print("\n")
print(dd,dd.index)

'''
**(1) 创建Series多层索引**
'''
# 创建Series多层索引
dd1 = pd.Series(np.random.randint(85,100,size=6),
                index=[['a','a','b','b','c','c'],
                       ['期中','期末','期中','期末','期中','期末']])
print("\n")
print(dd1,dd1.index,sep='\n')

'''
**(2) 利用 Series 创建DataFrame,添加列索引名**
'''
#利用 Series 创建DataFrame,添加列索引名
df = pd.DataFrame(dd1,columns=['python'])
print("\n")
print(df)

'''
**(3) DataFrame创建2级列索引(多层索引)(二维数组)**
'''
#DataFrame创建2级列索引(多层索引)(列表嵌套--二维数组)
name=['王下','昊一','杨玉','胡朋']
mode=['期中','期末']*3
course = ['python','python','math']*2
df = pd.DataFrame(np.random.randint(85,100,size=(4,6)),
                  index=name,
                  columns=[course,mode])
print("\n")
print(df)

'''
**(4)显式创建pd.MultiIndex多层索引,from_tuples---tuple构造**
'''
#DataFrame创建2级列索引(列表元素为元祖)---tuple构造
df1 = pd.DataFrame(np.random.randint(0,100,size=(4,6)),index=name,
               columns =pd.MultiIndex.from_tuples
               ([('python','期中'),('python','期末'),
                          ('math','期中'),('math','期末'),
                          ('English','期中'),('English','期末')]))
print("\n")
print(df1)

'''
**(5)显式创建pd.MultiIndex多层索引,from_arrays构造**
'''
#DataFrame创建2级列索引(列表元素为元祖)
state=['期中','期末']*3
df1 = pd.DataFrame(np.random.randint(0,100,size=(4,6)),
               index=name,
               columns =pd.MultiIndex.from_arrays
               ([['python','python','math','math','English','English'],
                          state]))
print("\n")
print(df1)

'''
**(6) 显式创建pd.MultiIndex多层索引
( 使用简单的from_product函数(自动进行交叉))**
'''
#使用.from_product构造多层列索引  
course = ['python','math','English',]
mode = ['期中','期末']
df2 = pd.DataFrame(np.random.randint(85,100,size=(4,6)),
               columns = pd.MultiIndex.from_product([course,mode]),
               index = name)
print("\n")
print(df2)

'''
**(7) 使用.from_product构造多层行索引**
'''
#使用.from_product构造多层行索引 
course = ['python','math','English',]
mode = ['期中','期末']
df3 = pd.DataFrame(np.random.randint(85,100,size=(8,6)),
               columns = pd.MultiIndex.from_product([['第一学期','第二学期'],course]),
               index =pd.MultiIndex.from_product([name,mode]))
print("\n")
print(df3)

'''
**(8) 调整层级索引的顺序(swaplevel())**
'''
df3.index.names=['key1','key2']  #为多层索引添加名称
ddf = df3.swaplevel('key2','key1')
print("\n")
print(ddf)

'''
**(9) 根据某级别的索引进行汇总(sum(level,axis))**
'''
print("\n")
print(df3)
df3.sum(level=1,axis=0)
ddf = df3.sum(level=0,axis=0)   
print("\n")
print(ddf)
ddf = df3.sum(level=1,axis=1)
print("\n")
print(ddf)
#测试!
print("\n")
ddf4=df3.sum(level=0,axis=1)
print(ddf4)

运行结果:

a    39
b    43
c    81
d    85
e    33
f    35
dtype: int32 Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')


a  期中    85
   期末    86
b  期中    99
   期末    98
c  期中    88
   期末    97
dtype: int32
MultiIndex([('a', '期中'),
            ('a', '期末'),
            ('b', '期中'),
            ('b', '期末'),
            ('c', '期中'),
            ('c', '期末')],
           )


      python
a 期中      85
  期末      86
b 期中      99
  期末      98
c 期中      88
  期末      97


   python     math python     math
       期中  期末   期中     期末  期中   期末
王下     92  91   92     90  85   87
昊一     92  96   85     90  88   94
杨玉     86  98   89     95  85   90
胡朋     92  88   97     92  98   94


   python     math     English    
       期中  期末   期中  期末  期中  期末
王下     52   0   13  49      93  92
昊一     77  16   46  85      58  81
杨玉     53  25   65  97       4  21
胡朋     53  13   58  83      25  86


   python     math     English    
       期中  期末   期中  期末  期中  期末
王下     89  82   43  18      39  29
昊一     56  29   95  78      61  89
杨玉      0  34   78  27      82  95
胡朋     64  57   57  65      26  76


   python     math     English    
       期中  期末   期中  期末  期中  期末
王下     94  99   93  99      96  85
昊一     93  92   98  93      95  86
杨玉     88  89   99  91      89  88
胡朋     85  88   87  90      93  91


                第一学期                第二学期             
           python math English python math English
王下  期中     12   67      19     94   55      97
     期末     77   84      91     17   71      75
昊一  期中     39   63      49     66   32      82
     期末     61   91       9     30   19      37
杨玉  期中     13   73      56     77   72      65
     期末     67   71      10     88   94      81
胡朋  期中      2   47      61     22   57      61
     期末      9   17       2     97   63      57


            第一学期                第二学期             
          python math English python math English
key2 key1                                        
期中   王下       12   67      19     94   55      97
期末   王下       77   84      91     17   71      75
期中   昊一       39   63      49     66   32      82
期末   昊一       61   91       9     30   19      37
期中   杨玉       13   73      56     77   72      65
期末   杨玉       67   71      10     88   94      81
期中   胡朋        2   47      61     22   57      61
期末   胡朋        9   17       2     97   63      57


                 第一学期                第二学期             
             python math English python math English
key1 key2                                        
王下   期中       12   67      19     94   55      97
      期末       77   84      91     17   71      75
昊一   期中       39   63      49     66   32      82
      期末       61   91       9     30   19      37
杨玉   期中       13   73      56     77   72      65
      期末       67   71      10     88   94      81
胡朋   期中        2   47      61     22   57      61
      期末        9   17       2     97   63      57


             第一学期                第二学期             
        python math English python math English
key1                                        
王下       89  151     110    111  126     172
昊一      100  154      58     96   51     119
杨玉       80  144      66    165  166     146
胡朋       11   64      63    119  120     118


           python  math  English
key1 key2                       
王下   期中       106   122      116
      期末        94   155      166
昊一   期中       105    95      131
      期末        91   110       46
杨玉   期中        90   145      121
      期末       155   165       91
胡朋   期中        24   104      122
      期末       106    80       59
      
           第一学期  第二学期
key1  key2            
王下   期中     265   268
      期末     284   269
昊一   期中     286   274
      期末     289   291
杨玉   期中     271   268
      期末     268   268
胡朋   期中     273   271
      期末     274   262

2.多层索引对象的索引与切片操作

"""
(1).Series的操作
Series来说,直接使用中括号[]与或使用.loc()
"""
import pandas as pd
import numpy as np

dd1 = pd.Series(np.random.randint(88,100,size=10),
                index=pd.MultiIndex.from_product
                ([list('abcde'),['期中','期末']]))
print("\n")
print(dd1)
print("\n")
#取指定索引的值
print(dd1['a']['期中'],dd1['a','期中'],dd1.loc['b']['期中'],'\n')
#利用外层索引的切片(切片只切外层(第一级)索引)
print(dd1[0:2])
print("\n")
print(dd1[2:8])

"""
(2). dataframe的索引与切片
"""
course = ['python','math','English',]
mode = ['期中','期末']
name = ['李玉','齐安','依依','鱼儿']
df3 = pd.DataFrame(np.random.randint(90,100,size=(8,6)),
               columns = pd.MultiIndex.from_product([['第一学期','第二学期'],
                                                     course]),
               index =pd.MultiIndex.from_product([name,mode]) )
print("\n")
print(df3)
print("\n")
# dataframe 切片
print(df3[0:3])
print("\n")
print(df3.iloc[2:4])
print("\n")
# dataframe 索引
print(df3['第一学期']['python'])
print("\n")
print(df3['第一学期'][['python','math']])
print("\n")
print(df3.loc['李玉','期中']['第一学期'])
#测试!
print("\n")
print(df3.loc['依依','期末']['第二学期'])

运行结果:

a  期中    88
   期末    95
b  期中    93
   期末    99
c  期中    99
   期末    97
d  期中    90
   期末    98
e  期中    88
   期末    90
dtype: int32

88 88 93 

a  期中    88
   期末    95
dtype: int32


b  期中    93
   期末    99
c  期中    99
   期末    97
d  期中    90
   期末    98
dtype: int32

               第一学期                第二学期             
          python math English python math English
李玉 期中     98   91      94     95   98      99
    期末     97   97      93     98   93      95
齐安 期中     99   94      99     98   98      98
    期末     98   90      99     91   94      93
依依 期中     91   94      93     94   94      90
    期末     90   99      93     94   95      90
鱼儿 期中     99   97      99     91   96      96
    期末     91   97      93     91   91      92


         第一学期                第二学期             
      python math English python math English
李玉 期中     98   91      94     95   98      99
    期末     97   97      93     98   93      95
齐安 期中     99   94      99     98   98      98


               第一学期                第二学期             
          python math English python math English
齐安 期中     99   94      99     98   98      98
    期末     98   90      99     91   94      93

李玉  期中    98
     期末    97
齐安  期中    99
     期末    98
依依  期中    91
     期末    90
鱼儿  期中    99
     期末    91
Name: python, dtype: int32

       python  math
李玉 期中      98    91
    期末      97    97
齐安 期中      99    94
    期末      98    90
依依 期中      91    94
    期末      90    99
鱼儿 期中      99    97
    期末      91    97

python     98
math       91
English    94
Name: (李玉, 期中), dtype: int32

python     99
math       93
English    92
Name: (依依, 期末), dtype: int32

四、pandas 数据结构之间的运算

1.数据对齐与算术运算

"""
pandas的数据对齐: 当参与运算的两个数据结构,尽管其索引项的顺序不一致,
或者有的索引只存在于一个数据结构中时,也能实现这两个数据结构之间的运算。
"""
import numpy as np
from pandas import DataFrame,Series
print("\n")
print ('定义两个Series对象')
se1 = Series([2.1, 4.3, -1.6], index = ['c', 'd', 'a'])
se2 = Series(np.arange(4), index = ['a', 'b', 'c', 'd'])
print(se1,se2,sep='\n')
#将两个Series对象相加
print('两个Series对象相加\n',se1+se2)
print('两个Series对象相减\n',se1-se2)
print('两个Series对象相乘\n',se1*se2)
print('两个Series对象相除\n',se1/se2)
print('两个Series对象求余\n',se1%se2)

import numpy as np
from pandas import DataFrame,Series
print("\n")
print ('定义两个DataFrame')
df1 = DataFrame(np.arange(6).reshape(2,3), columns=['a','b','c'])
df2 = DataFrame(np.arange(12).reshape(3,4),columns=['a','b','c','d'])
print('df1',df1,'df2',df2,sep='\n')

#将两个DataFrame对象进行加、减、乘、除运算
print('两个DataFrame对象相加\n',df1+df2)
print('两个DataFrame对象相减\n',df1-df2)
print('两个DataFrame对象相乘\n',df1*df2)
print('两个DataFrame对象相除\n',df1/df2)

import numpy as np
from pandas import DataFrame,Series
print("\n")
print ('定义两个DataFrame')
df1 = DataFrame(np.arange(6).reshape(2,3), 
                columns=['a','b','c'],
               index=['D','B'])
df2 = DataFrame(np.arange(12).reshape(3,4),
                columns=['a','b','c','d'],
               index=['B','C','D'])
print('df1',df1,'df2',df2,sep='\n')

#将两个DataFrame对象进行加、减、乘、除运算
print('两个DataFrame对象相加\n',df1+df2)
print('两个DataFrame对象相减\n',df1-df2)
print('两个DataFrame对象相乘\n',df1*df2)
print('两个DataFrame对象相除\n',df1/df2)

运行结果 :

定义两个Series对象
c    2.1
d    4.3
a   -1.6
dtype: float64
a    0
b    1
c    2
d    3
dtype: int32
两个Series对象相加
 a   -1.6
b    NaN
c    4.1
d    7.3
dtype: float64
两个Series对象相减
 a   -1.6
b    NaN
c    0.1
d    1.3
dtype: float64
两个Series对象相乘
 a    -0.0
b     NaN
c     4.2
d    12.9
dtype: float64
两个Series对象相除
 a        -inf
b         NaN
c    1.050000
d    1.433333
dtype: float64
两个Series对象求余
 a    NaN
b    NaN
c    0.1
d    1.3
dtype: float64


定义两个DataFrame
df1
   a  b  c
0  0  1  2
1  3  4  5
df2
   a  b   c   d
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
两个DataFrame对象相加
      a    b     c   d
0  0.0  2.0   4.0 NaN
1  7.0  9.0  11.0 NaN
2  NaN  NaN   NaN NaN
两个DataFrame对象相减
      a    b    c   d
0  0.0  0.0  0.0 NaN
1 -1.0 -1.0 -1.0 NaN
2  NaN  NaN  NaN NaN
两个DataFrame对象相乘
       a     b     c   d
0   0.0   1.0   4.0 NaN
1  12.0  20.0  30.0 NaN
2   NaN   NaN   NaN NaN
两个DataFrame对象相除
       a    b         c   d
0   NaN  1.0  1.000000 NaN
1  0.75  0.8  0.833333 NaN
2   NaN  NaN       NaN NaN


定义两个DataFrame
df1
   a  b  c
D  0  1  2
B  3  4  5
df2
   a  b   c   d
B  0  1   2   3
C  4  5   6   7
D  8  9  10  11
两个DataFrame对象相加
      a     b     c   d
B  3.0   5.0   7.0 NaN
C  NaN   NaN   NaN NaN
D  8.0  10.0  12.0 NaN
两个DataFrame对象相减
      a    b    c   d
B  3.0  3.0  3.0 NaN
C  NaN  NaN  NaN NaN
D -8.0 -8.0 -8.0 NaN
两个DataFrame对象相乘
      a    b     c   d
B  0.0  4.0  10.0 NaN
C  NaN  NaN   NaN NaN
D  0.0  9.0  20.0 NaN
两个DataFrame对象相除
      a         b    c   d
B  inf  4.000000  2.5 NaN
C  NaN       NaN  NaN NaN
D  0.0  0.111111  0.2 NaN

2.算术运算方法

"""
加:add()、减:sub()、乘:mul()、除:div()、求余mod()等函数。 
函数的调用方法: obj1. add(obj2)
"""

import numpy as np
from pandas import DataFrame,Series
print("\n")
print ('定义两个DataFrame')
df1 = DataFrame(np.arange(6).reshape(2,3), 
                columns=['a','b','c'],
               index=['D','B'])
df2 = DataFrame(np.arange(12).reshape(3,4),
                columns=['a','b','c','d'],
               index=['B','C','D'])
print('df1',df1,'\n','df2',df2)
#将两个DataFrame对象进行加、减、乘、除、求余运算
print('两个DataFrame对象相加\n',df1.add(df2))
print('两个DataFrame对象相减\n',df1.sub(df2))
print('两个DataFrame对象相乘\n',df1.mul(df2))
print('两个DataFrame对象相除\n',df1.div(df2))
print('两个DataFrame对象求余\n',df1.mod(df2))


import numpy as np
from pandas import DataFrame,Series
print("\n")
print ('定义Series对象')
se1 = Series([10,20,30,40], index = ['a', 'b', 'c', 'd'])
print(se1)
print ('定义DataFrame对象')
df1 = DataFrame(np.arange(6).reshape(2,3), columns=['a','b','c'])
print(df1)

#将Series与DataFrame对象进行加、减、乘、除运算
print('相加\n',df1+se1)
print('相减\n',df1-se1)
print('相乘\n',df1*se1)
print('相除\n',df1/se1)

运行结果:

定义两个DataFrame
df1    a  b  c
D  0  1  2
B  3  4  5 
 df2    a  b   c   d
B  0  1   2   3
C  4  5   6   7
D  8  9  10  11
两个DataFrame对象相加
      a     b     c   d
B  3.0   5.0   7.0 NaN
C  NaN   NaN   NaN NaN
D  8.0  10.0  12.0 NaN
两个DataFrame对象相减
      a    b    c   d
B  3.0  3.0  3.0 NaN
C  NaN  NaN  NaN NaN
D -8.0 -8.0 -8.0 NaN
两个DataFrame对象相乘
      a    b     c   d
B  0.0  4.0  10.0 NaN
C  NaN  NaN   NaN NaN
D  0.0  9.0  20.0 NaN
两个DataFrame对象相除
      a         b    c   d
B  inf  4.000000  2.5 NaN
C  NaN       NaN  NaN NaN
D  0.0  0.111111  0.2 NaN
两个DataFrame对象求余
      a    b    c   d
B  NaN  0.0  1.0 NaN
C  NaN  NaN  NaN NaN
D  0.0  1.0  2.0 NaN


定义Series对象
a    10
b    20
c    30
d    40
dtype: int64
定义DataFrame对象
   a  b  c
0  0  1  2
1  3  4  5
相加
     a   b   c   d
0  10  21  32 NaN
1  13  24  35 NaN
相减
     a   b   c   d
0 -10 -19 -28 NaN
1  -7 -16 -25 NaN
相乘
     a   b    c   d
0   0  20   60 NaN
1  30  80  150 NaN
相除
      a     b         c   d
0  0.0  0.05  0.066667 NaN
1  0.3  0.20  0.166667 NaN
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KirinLee_01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值