2016-08-30 70 views
0

我有一个熊猫数据框,它是一个布尔值的正方形网格,索引和列是具有邻居的628个选区的名称。例如在pandas中返回列名匹配标准

In[18]: adjacents.index 
Out[18]: 
Index(['Aberavon', 'Aberconwy', 'Aberdeen North', 'Aberdeen South', 
     'Aberdeenshire West & Kincardine', 'Airdrie & Shotts', 'Aldershot', 
     'Aldridge-Brownhills', 'Altrincham & Sale West', 'Alyn & Deeside', 
     ... 
     'Wrekin, The', 'Wrexham', 'Wycombe', 'Wyre & Preston North', 
     'Wyre Forest', 'Wythenshawe & Sale East', 'Yeovil', 'York Central', 
     'York Outer', 'Yorkshire East'], 
     dtype='object', length=628) 

In[19]: adjacents.columns 
Out[19]: 
Index(['Aberavon', 'Aberconwy', 'Aberdeen North', 'Aberdeen South', 
     'Aberdeenshire West & Kincardine', 'Airdrie & Shotts', 'Aldershot', 
     'Aldridge-Brownhills', 'Altrincham & Sale West', 'Alyn & Deeside', 
     ... 
     'Wrekin, The', 'Wrexham', 'Wycombe', 'Wyre & Preston North', 
     'Wyre Forest', 'Wythenshawe & Sale East', 'Yeovil', 'York Central', 
     'York Outer', 'Yorkshire East'], 
     dtype='object', length=628) 

所以,曼彻斯特中心的便利是毗邻曼彻斯特戈顿,但不是曼彻斯特阿丁顿。

In[20]: adjacents['Manchester Central']['Manchester Withington'] 
Out[20]: False 

In[21]: adjacents['Manchester Central']['Manchester Gorton'] 
Out[21]: True 

什么是最好的方式返回列中的所有列的列名是True? (我想这可能是是与np.argmax()但不能完全弄明白。)

回答

1

嗯,你能做到这一点,但因为你的矩阵似乎是symmetric你会更好找到行名字代替,因为这是更简单:

adjacents[adjacents['Manchester Central']].index 

顺便说一句 - 在访问表中的确切位置时,最好的做法是使用loc

adjacents.loc['Manchester Central', 'Manchester Gorton']