【Python - 100天从新手到大师】——pandas索引

原文链接:https://github.com/jackfrued/Python-100-Days

【Python - 100天从新手到大师】

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
s = Series(np.random.randint(0,150,size = 100),index = np.arange(10,110),dtype=np.int16,name = 'Python')
s

》》》 10      34
	  11     111
	  12     113
	  13     103
	  14     147
	  15      63
	  16      11
	  17     130
	  18      38
	  19      17
	  20      32
	  21     112
	  22      75
	  23      68
	  24     124
	  25     138
	  26      56
	  27       1
	  28      88
	  29     113
	  30      63
	      ... 
	  90     110
	  91     144
	  92      40
	  93       3
	  94       3
	  95      59
	  96      97
	  97      64
	  98     126
	  99      94
	  100     20
	  101    107
	  102     59
	  103    146
	  104     83
	  105     59
	  106     25
	  107      0
	  108     78
	  109     93
	  Name: Python, Length: 100, dtype: int16
# 索引操作
s[0]

》》》 KeyError: 0
s[10]

》》》 34
s[[10,20]]

》》》 10    34
	  20    32
	  Name: Python, dtype: int16
# 切片操作
s[10:20]

》》》 20     32
	  21    112
	  22     75
	  23     68
	  24    124
	  25    138
	  26     56
	  27      1
	  28     88
	  29    113
	  Name: Python, dtype: int16
s[::2]

》》》 10      34
	  12     113
	  14     147
	  16      11
	  18      38
	  20      32
	  22      75
	  24     124
	  26      56
	  28      88
	  30      63
		...
	  80     127
	  82     110
	  84     127
	  86      33
	  88     134
	  90     110
	  92      40
	  94       3
	  96      97
	  98     126
	  100     20
	  102     59
	  104     83
	  106     25
	  108     78
	  Name: Python, dtype: int16
s[::-2]

》》》 109     93
	  107      0
	  105     59
	  103    146
	  101    107
	  99      94
	  97      64
	  95      59
	  93       3
	  91     144
	  ...
	  31      42
	  29     113
	  27       1
	  25     138
	  23      68
	  21     112
	  19      17
	  17     130
	  15      63
	  13     103
	  11     111
	  Name: Python, dtype: int16
# 可以使用pandas为开发者提供方法,去进行检索
s.loc[10]

》》》 34
s.loc[[10,20]]

》》》 10    34
	  20    32
	  Name: Python, dtype: int16
s.loc[10:20]

》》》 10     34
	  11    111
	  12    113
	  13    103
	  14    147
	  15     63
	  16     11
	  17    130
	  18     38
	  19     17
	  20     32
	  Name: Python, dtype: int16
s.loc[::2]

》》》 10      34
	  12     113
	  14     147
	  16      11
	  18      38
	  20      32
	  22      75
	  24     124
	  26      56
	  28      88
	  30      63
	  ...
	  90     110
	  92      40
	  94       3
	  96      97
	  98     126
	  100     20
	  102     59
	  104     83
	  106     25
	  108     78
	  Name: Python, dtype: int16
s.loc[::-2]

》》》 109     93
	  107      0
	  105     59
	  103    146
	  101    107
	  99      94
	  97      64
	  95      59
	  93       3
	  91     144
	  ...
	  31      42
	  29     113
	  27       1
	  25     138
	  23      68
	  21     112
	  19      17
	  17     130
	  15      63
	  13     103
	  11     111
	  Name: Python, dtype: int16
s.index

》》》 Int64Index([ 10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,
             	   23,  24,  25,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,
             	   36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
             	   49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,
             	   62,  63,  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,
             	   75,  76,  77,  78,  79,  80,  81,  82,  83,  84,  85,  86,  87,
             	   88,  89,  90,  91,  92,  93,  94,  95,  96,  97,  98,  99, 100,
            	   101, 102, 103, 104, 105, 106, 107, 108, 109],
           	  	   dtype='int64')
# iloc 索引从0开始,数字化自然索引
s.iloc[0]

》》》 34
s.iloc[[0,10]]

》》》 10    34
	  20    32
	  Name: Python, dtype: int16
s.iloc[0:20]

》》》 10     34
	  11    111
	  12    113
	  13    103
	  14    147
	  15     63
	  16     11
	  17    130
	  18     38
	  19     17
	  20     32
	  21    112
	  22     75
	  23     68
	  24    124
	  25    138
	  26     56
	  27      1
	  28     88
	  29    113
	  Name: Python, dtype: int16
s.iloc[::-2]

》》》 109     93
	  107      0
	  105     59
	  103    146
	  101    107
	  99      94
	  97      64
	  95      59
	  93       3
	  91     144
  	  ...
	  29     113
	  27       1
	  25     138
	  23      68
	  21     112
 	  19      17
	  17     130
	  15      63
	  13     103
	  11     111
	  Name: Python, dtype: int16
# DataFrame是二维,索引大同小异,
df = DataFrame(data = np.random.randint(0,150,size= (10,3)),index=list('ABCDEFHIJK'),columns=['Python','En','Math'])
df

在这里插入图片描述

df['A']

》》》 KeyError: 'A'
df['Python']

》》》 A    103
	  B    135
	  C     13
	  D     47
	  E     89
	  F     64
	  H     48
	  I     16
	  J    122
	  K     60
	  Name: Python, dtype: int32
df[['Python','En']]

在这里插入图片描述

df['Python':'Math']

在这里插入图片描述

df['A':'D']

在这里插入图片描述

df.loc['Python']

》》》 KeyError: 'Python'
df.loc['A']

》》》 Python    103
	  En         56
	  Math       98
	  Name: A, dtype: int32
df.loc[['A','H']]

在这里插入图片描述

df.loc['A':'E']

在这里插入图片描述

df.loc[::2]

在这里插入图片描述

df.loc[::-2]

在这里插入图片描述

df.iloc['A']

》》》 TypeError: Cannot index by location index with a non-integer key
df.iloc[0]

》》》 Python    103
	  En         56
	  Math       98
	  Name: A, dtype: int32
df.iloc[[0,5]]

在这里插入图片描述

df.iloc[0:5]

在这里插入图片描述

df.iloc[::-2]

在这里插入图片描述

df

在这里插入图片描述

df.iloc[::2,1:]

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/TeFuirnever/article/details/94649974