1,选择列
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
df = DataFrame(np.random.rand(12).reshape((3,4)),
index = ['one', 'two', 'three'],
columns= list('abcd')
)
print(df)
type(df['a'])
df[['a','c']] # dataframe
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'] # index 标签切片,闭区间
# df.loc[label] 主要是针对行索引,同时指出指定index,及默认的index
(未完待续,原文地址:https://www.cnblogs.com/gt92/p/11803711.html)