数据框索引行

label1=['apple','orange','pear','banana']
label2=['one','two','three','four']


df=pd.DataFrame(np.arange(16).reshape(4,-1),index=label1,columns=label2)

print(df)

print(df[['one','three']])  #虽然只传入一个字符串列表,并未指定任何东西,它是取指定的列
print(df[['apple','pear']])  #这种并不能索引出想要的行,它结果会报错,
# KeyError: "None of [Index(['apple', 'pear'], dtype='object')] are in the [columns]"
原文地址:https://www.cnblogs.com/bravesunforever/p/12764676.html