python

超轻量级php框架startmvc

pandas全表查询定位某个值所在行列的方法

更新时间:2020-05-28 11:42:01 作者:startmvc
如下所示:#createadataframewithanintegerfeatureandacategoricalstringfeaturedemo_df=pd.DataFrame({'IntegerFeature':[

如下所示:


# create a dataframe with an integer feature and a categorical string feature
demo_df = pd.DataFrame({'Integer Feature': [0, 1, 2, 1], 'Categorical Feature': ['socks', 'fox', 'socks', 'box']})
demo_df

接下来用for遍历:


for indexs in demo_df.index: 
 for i in range(len(demo_df.loc[indexs].values)): 
 if(demo_df.loc[indexs].values[i] =='fox'): 
 print(indexs,i) 
 print(demo_df.loc[indexs].values[i]) 

或者用列表推导式:


Categorical FeatureInteger Feature0socks01fox12socks23box1 
[ (indexs,i) for indexs in demo_df.index for i in range(len(demo_df.loc[indexs].values)) if(demo_df.loc[indexs].values[i] =='fox')] 

以上这篇pandas全表查询定位某个值所在行列的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

pandas 行列