1. 多条件筛选
import pandas as pd
data=[['mark',55,'Italy',4.5,'Europe'],
['John',33,'China',3.8,'Asian'],
['mary',40,'Japan',2.3,'Asian']]
df=pd.DataFrame(data=data,columns=['name','age','country','score','continent'],
index=[1001,1002,1003])
criterial=(df['age']>35)&(df['country']=='Japan')
print(df[criterial])
2. 判断在series中是是否包含某些对象
print(df.loc[df['country'].isin(['China','Italy']),:])
3. 使用multiindex选取数据
import pandas as pd
data=[['mark',55,'Italy',4.5,'Europe'],
['John',33,'China',3.8,'Asian'],
['mary',40,'Japan',2.3,'Asian']]
df=pd.DataFrame(data=data,columns=['name','age','country','score','continent'],
index=[1001,1002,1003])
df_multi=df.set_index(['continent','country'])
print(df_multi)
print(df_multi.loc['Asian'])
*pd.reset_index(level=None, , drop=False, inplace=False, col_level=0, col_fill=‘’, allow_duplicates=_NoDefault.no_default, names=None)
level:移除指定层级的index
drop: 重新设置索引后是否将原索引作为新的一列并入DataFrame,默认为False
inplace: 是否在原DataFrame上改动,默认为False