说明:记录阅读代码过程中的布尔索引
布尔索引
data = [['Tom', 86], ['Bob', 80], ['Lily', 90], ['Mike', 97]]
df = pd.DataFrame(data,columns=['Name','Score'])
df
df['Name'][df['Score']>85]
记录报错 KeyError
data = [['Tom', 86], ['Bob', 80], ['Lily', 90], ['Mike', 97]]
df = pd.DataFrame(data,columns=[0, 1])
df
df[['0']]
# KeyError: "None of [Index(['0'], dtype='object')] are in the [columns]"
解决步骤
- 查看索引是否正确:注意列名的数据类型:
type(df.columns[0])
,0表示第1个列 - 查看索引语句是否正确:
df.loc[:, '0']
返回Series