返回pandas数据帧中特定值的列名

时间:2016-07-12 14:21:57

标签: python pandas

我在其他语言(例如R或SQL)中找到了此选项,但我不太确定如何在Pandas中进行此操作。

所以我有一个包含1262列和1行的文件,并且每次出现特定值时都需要返回列标题。

比如说这个测试数据帧:

Date               col1    col2    col3    col4    col5    col6    col7 
01/01/2016 00:00   37.04   36.57   35.77   37.56   36.79   35.90   38.15 

我需要找到例如列名称,例如其中value = 38.15。这样做的最佳方式是什么?

由于

4 个答案:

答案 0 :(得分:12)

看到你只有一行,那么你可以在结果上调用iloc[0]并使用它来掩盖列:

In [47]:
df.columns[(df == 38.15).iloc[0]]

Out[47]:
Index(['col7'], dtype='object')

分解以上内容:

In [48]:
df == 38.15

Out[48]:
             Date   col1   col2   col3   col4   col5   col6  col7
01/01/2016  False  False  False  False  False  False  False  True

In [49]:
(df == 38.15).iloc[0]

Out[49]:
Date    False
col1    False
col2    False
col3    False
col4    False
col5    False
col6    False
col7     True
Name: 01/01/2016, dtype: bool

您还可以将idxmax与参数axis=1一起使用:

In [52]:
(df == 38.15).idxmax(axis=1)[0]

Out[52]:
'col7'

答案 1 :(得分:2)

您可以使用数据框切片,然后获取列名称:

Index([u'col7'], dtype='object')

输出:

#include "Arduino.h"
#include <WiFi101.h>
#include <SPI.h>
#include <WebSocketClient.h>

char server[] = "192.168.2.128";
IPAddress server1(192, 168, 2, 128);
WebSocketClient webSocketClient;
WiFiClient client;

char path1[] = "/api/WebSocketTest";
int port1 = 8060;
char ssid[] = "**************";
char pass[] = "**************";

void setup() {
  pinMode(LDR, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(SWITCH, INPUT);
  Serial.begin(115200);
  delay(10);
  while (!Serial) {
  }

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  printWifiStatus();
  delay(3000);
  Serial.println("setup done");
  delay(1000);


  //  webSocketClient.setDebug(&debug);
  webSocketClient.connect(server, port1, path1);
  if (webSocketClient.connected() == true) {
    Serial.println("Connected");
  }
  else {
    Serial.println("Not Connected");
  }
  webSocketClient.onMessage(onMessage);
  webSocketClient.onOpen(onOpen);
  webSocketClient.onError(onError);
}




void loop() {
  webSocketClient.monitor();
}




void onOpen(WebSocketClient webSocketClient) {
  Serial.println("EXAMPLE: onOpen()");
}



void onMessage(WebSocketClient webSocketClient, char* message) {
  Serial.print("Received: ");
  Serial.println(message);
}



void onError(WebSocketClient webSocketClient, char* message) {
  Serial.print("ERROR: ");
  Serial.println(message);
}




void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

答案 2 :(得分:0)

只是为了在戒指中投入一些不同的东西:

row = df.iloc[0]
row.reset_index().set_index(0).loc[38.15]

答案 3 :(得分:0)

假设我们有这个df。仅检查df的前三行,我们想要获取特定值为5的列的名称。

df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))
    df.head(3)

我们可以这样做:

In[61]:
for index, row in df[:3].iterrows():
    for i in range(len(df.columns)): 
        if row[i] == 5:
            print(row.index[i])
Out[61]:
'D'