来源:python中国网 时间:2019-07-20

  行标签和列标签其实都是一种索引。

  一:选择列

  选择一列就是Series,选择多列就是DataFrame

# -*- coding: utf-8 -*-
import pandas as pd

d = {'col1': [1,2,3], 'col2': [4,5,6],'col3':[7,8,9]}
df = pd.DataFrame(data=d)
print(df)
print('-----------------')

print(df['col1'])
print(df[['col2','col1']])  # 两个中括号
D:python3installpython.exe D:/python/py3script/test.py
   col1  col2  col3
0     1     4     7
1     2     5     8
2     3     6     9
-----------------
0    1
1    2
2    3
Name: col1, dtype: int64
   col2  col1
0     4     1
1     5     2
2     6     3

Process finished with exit code 0


  二:选择行

  1、loc实现(末端包含)标签索引

  2、切片(末端不包含)

  3、iloc实现(末端不包含)行位置索引

# -*- coding: utf-8 -*-
import pandas as pd

d = {'col1': [1,2,3], 'col2': [4,5,6],'col3':[7,8,9]}
df = pd.DataFrame(data=d)
print(df)
print('-----------------')

# loc实现
print(df.loc[[1,2]])
print('-------------')

#切片
print(df[0:2])
print('--------------')

#iloc实现
print(df.iloc[0:2])
D:python3installpython.exe D:/python/py3script/test.py
   col1  col2  col3
0     1     4     7
1     2     5     8
2     3     6     9
-----------------
   col1  col2  col3
1     2     5     8
2     3     6     9
-------------
   col1  col2  col3
0     1     4     7
1     2     5     8
--------------
   col1  col2  col3
0     1     4     7
1     2     5     8

Process finished with exit code 0


总结:
df['name1']
df['name2']
df[['name1','name2']] #选取多列,多列名字要放在list里
df[0:]  #第0行及之后的行,相当于df的全部数据,注意冒号是必须的
df[:2]  #第2行之前的数据(不含第2行)
df[0:1] #第0行
df[1:3] #第1行到第2行(不含第3行)
df[-1:] #最后一行
df[-3:-1] #倒数第3行到倒数第1行(不包含最后1行即倒数第1行,从前数时从第0行开始,从后数就是-1行开始,没有-0)


  三:布尔型索引

  1、单列做判断

# -*- coding: utf-8 -*-
import pandas as pd

d = {'col1': [1,2,3], 'col2': [4,5,6],'col3':[7,8,9]}
df = pd.DataFrame(data=d)
print(df)
print('-------------------')

res = df['col2'] > 5
print(res)
print('---------------------')
print(df[res])
D:python3installpython.exe D:/python/py3script/test.py
   col1  col2  col3
0     1     4     7
1     2     5     8
2     3     6     9
-------------------
0    False
1    False
2     True
Name: col2, dtype: bool
---------------------
   col1  col2  col3
2     3     6     9

Process finished with exit code 0



  2、多列做判断

# -*- coding: utf-8 -*-
import pandas as pd

d = {'col1': [1,2,3], 'col2': [4,5,6],'col3':[7,8,9]}
df = pd.DataFrame(data=d)
print(df)
print('-------------------')

res = df > 5
print(res)
print('---------------------')
print(df[res])
D:python3installpython.exe D:/python/py3script/test.py
   col1  col2  col3
0     1     4     7
1     2     5     8
2     3     6     9
-------------------
    col1   col2  col3
0  False  False  True
1  False  False  True
2  False   True  True
---------------------
   col1  col2  col3
0   NaN   NaN     7
1   NaN   NaN     8
2   NaN   6.0     9

Process finished with exit code 0



  3、多行做判断

# -*- coding: utf-8 -*-
import pandas as pd

d = {'col1': [1,2,3], 'col2': [4,5,6],'col3':[7,8,9]}
df = pd.DataFrame(data=d)
print(df)
print('-------------------')

res = df.loc[[1,2]] > 5
print(df[res])
D:python3installpython.exe D:/python/py3script/test.py
   col1  col2  col3
0     1     4     7
1     2     5     8
2     3     6     9
-------------------
   col1  col2  col3
0   NaN   NaN   NaN
1   NaN   NaN   8.0
2   NaN   6.0   9.0

Process finished with exit code 0



  四:多重索引

  先选择行再选择列且配合布尔判断

# -*- coding: utf-8 -*-
import pandas as pd

d = {'col1': [1,2,3], 'col2': [4,5,6],'col3':[7,8,9]}
df = pd.DataFrame(data=d)
print(df)
print('-------------------')

v = df['col1'].loc[[1,2]]
print(v)
print('--------------')

v = df.loc[1:2]['col1']
print(v)
print('--------------')
print(df[df > 5].loc[1:2]['col1'])
D:python3installpython.exe D:/python/py3script/test.py
   col1  col2  col3
0     1     4     7
1     2     5     8
2     3     6     9
-------------------
1    2
2    3
Name: col1, dtype: int64
--------------
1    2
2    3
Name: col1, dtype: int64
--------------
1   NaN
2   NaN
Name: col1, dtype: float64

Process finished with exit code 0