DataFrame的索引和基本操作
一、DataFrame的索引 1,选择列 1 import pandas as pd 2 import numpy as np 3 from pandas import Series, DataFrame 4 5 df = DataFrame(np.random.rand(12).reshape((3,4)), 6 index = ['one', 'two', 'three'], 7 columns= list('abcd') 8 ) 9 print(df) 10 type(df['a']) 11 df[['a','c']] # dataframe 注:df[ ]--选择列,整数可以选择行,但是不能单独选择,要用切片的方式如df[:2] 2,选择行 df1 = DataFrame(np.random.rand(12).reshape((3,4)), index = [3,2,1], columns= list('abcd') ) print(df1) df.loc['one'] # 单独的行,返回的也是一个Series对象 # df1.loc[0] # 整数的索引,只能是index 是默认的整数时 df.loc[['one','three','four']] # 多行,返回的也是一个Dataframe对象 df1.loc[2:1] # df.loc['two':'three'] #