返回匹配pandas中的条件的列名

时间:2016-08-30 15:05:54

标签: pandas

我有一个pandas数据框,它是一个布尔值的方格,索引和列是628个选区的名字,这些选区有邻居。例如

protected override bool Receive(object message)
{
    // if the message is a previously persisted event, update our internal list
    var e = message as MyEvent;
    if (e != null) _events.Add(e);
    return true;

    // if the message is a request for a view model, read from our list of stored events
    var r = message as ReadRequest;
    if (r == null) return false;
    Sender.Tell(new ViewModel(_events));
    return true;
}

因此,曼彻斯特中心的比赛与曼彻斯特戈顿相邻,但不是曼彻斯特的英格兰队。

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有关,但无法弄明白。)

1 个答案:

答案 0 :(得分:1)

嗯,你可以这样做,但由于你的矩阵似乎是symmetric,你最好找到行名,因为这更简单:

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

BTW - 访问表格中的确切位置时,最好使用loc

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