简体   繁体   English

在 Pandas 中查找包含另一列行中特定值的列名

[英]Find column name in Pandas that contains a specific value in the row from another column

I have 200 plus columns by 10000 plus rows.我有 200 多列乘以 10000 多行。 I am trying to find the name of the column that contains a specific value in the row.我正在尝试查找包含行中特定值的列的名称。 In the example below, the specific value is in the value column.在下面的示例中,特定值位于值列中。 How I identified the value in the 'Value' column is not important.我如何确定“值”列中的值并不重要。

Example: The Value_Col is the value I am trying to return.示例: Value_Col 是我试图返回的值。

Date   Time  A     B     C     D     E     F     Value   Value_Col
Jan1   1245  3.0   3.2   4.6   5.7   2.1   8.0   5.7     D
Jan2   1045  4.5   8.4   3.9   2.2   9.4   8.3   3.9     C
Jan3   1350  1.4   3.3   4.5   8.9   1.4   0.4   1.4     A

I want to search only columns A through F and find the column name for the first instance (leftmost) the value exists.我只想搜索 A 到 F 列并找到值存在的第一个实例(最左边)的列名。 In the example, my value of interest appears twice in the row beginning with the Date of Jan3.在该示例中,我的兴趣值在以 1 月 3 日开始的行中出现了两次。 I want to basically index the column names the value appears and select the first one.我想基本上索引值出现的列名并选择第一个。 I understand this would be index 0 (or [0]) based on the list that is returned for the value search.我知道这将是基于为值搜索返回的列表的索引 0(或 [0])。

The example above is only a small subset of data.上面的例子只是一小部分数据。 I currently have a list of all column names I want the value search to occur in. The value of interest can occur through many of the columns in the same row.我目前有一个所有列名称的列表,我希望在其中进行值搜索。感兴趣的值可以通过同一行中的许多列出现。

I want to search only columns A through F and find the column name for the first instance (leftmost) the value exists我只想搜索列 A 到 F 并找到值存在的第一个实例(最左侧)的列名

You can use idxmax on axis=1 after comparing Value column with the slice of the datframe (using .loc[] )在将Value列与数据框的切片进行比较后,您可以在axis=1上使用idxmax (使用.loc[]

df['Value_Col'] = df.loc[:,'A':'F'].isin(df['Value']).idxmax(1)
print(df)

   Date  Time    A    B    C    D    E    F  Value Value_Col
0  Jan1  1245  3.0  3.2  4.6  5.7  2.1  8.0    5.7         D
1  Jan2  1045  4.5  8.4  3.9  2.2  9.4  8.3    3.9         C
2  Jan3  1350  1.4  3.3  4.5  8.9  1.4  0.4    1.4         A

If there are chances that none of the column may contain the df['Value] value , you can use:如果有可能没有列可能包含df['Value] value ,您可以使用:

m = df.loc[:,'A':'F']
df['Value_Col'] = m.isin(df['Value']).dot(m.columns).str[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM