python数据处理相关操作——选取数据
iloc,loc,ix
import pandas as pd
from pandas import DataFrame
创建数据框
data = {'a':[11,22,33,44],
'b':['aa','bb','cc','dd'],
'c':[9,8,7,6],
'd':[1,2,3,4]
}
df = DataFrame(data)
df
a b c d 0 11 aa 9 1 1 22 bb 8 2 2 33 cc 7 3 3 44 dd 6 4
iloc:通过行/列号选取数据
df.iloc[0] #选取第0行数据
a 11
b aa
c 9
d 1
Name: 0, dtype: object
df.iloc[0:2] #选取多行
a b c d 0 11 aa 9 1 1 22 bb 8 2
df.iloc[:,[1]] #也可以按照列号选取某列 选取第2列
b 0 aa 1 bb