import pandas as pd
import numpy as np
arr = np. random. randn( 3 , 3 )
arr
array([[-0.4840499 , -1.10358516, -2.02087998],
[ 1.47393252, -0.64985477, -0.11712859],
[-0.44069225, -0.37788616, 0.56497586]])
df = pd. DataFrame( arr, index= ( 'a' , 'b' , 'c' ) , columns= ( 'A' , 'B' , 'C' ) )
df
A B C a 1.440345 0.702222 1.268813 b 0.268986 0.954185 -0.273460 c -1.187953 0.717754 0.083791
[ ]切片方法
df[ 'a' : 'b' ]
A B C a 1.440345 0.702222 1.268813 b 0.268986 0.954185 -0.273460
df[ [ 'A' , 'C' ] ]
A C a 1.440345 1.268813 b 0.268986 -0.273460 c -1.187953 0.083791
df[ 'b' : 'c' ] [ [ 'B' , 'C' ] ]
B C b 0.954185 -0.273460 c 0.717754 0.083791
loc函数(使用索引值)
df. loc[ 'a' : 'b' ]
A B C a 1.440345 0.702222 1.268813 b 0.268986 0.954185 -0.273460
df. loc[ : , [ 'A' , 'C' ] ]
A C a 1.440345 1.268813 b 0.268986 -0.273460 c -1.187953 0.083791
df. loc[ 'a' : 'b' , [ 'B' , 'C' ] ]
B C a 0.702222 1.268813 b 0.954185 -0.273460
iloc函数(使用索引位置)
df. iloc[ 0 : 1 ]
A B C a 1.440345 0.702222 1.268813
df. iloc[ [ 0 ] ]
A B C a 1.440345 0.702222 1.268813
df. iloc[ : , 0 : 1 ]
A a 1.440345 b 0.268986 c -1.187953
df. iloc[ 0 : 2 , 0 : 2 ]
A B a 1.440345 0.702222 b 0.268986 0.954185
at函数(使用索引值访问单个元素)
df. at[ 'a' , 'A' ]
1.4403454841699048
iat函数(使用索引位置访问单个元素)
df. iat[ 0 , 0 ]
1.4403454841699048
ix函数(loc与iloc的混合)
df. ix[ 0 : 2 , 'A' : 'C' ]
/Users/f7689781/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
"""Entry point for launching an IPython kernel.
A B C a -0.484050 -1.103585 -2.020880 b 1.473933 -0.649855 -0.117129