返回 Pandas 数据框中特定值的列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38331568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return the column name(s) for a specific value in a pandas dataframe
提问by Helena K
where I have found this option in other languages such as R or SQL but I am not quite sure how to go about this in Pandas.
我在其他语言(如 R 或 SQL)中找到了此选项,但我不太确定如何在 Pandas 中进行此操作。
So I have a file with 1262 columns and 1 row and need the column headers to return for every time that a specific value appears.
所以我有一个包含 1262 列和 1 行的文件,并且每次出现特定值时都需要返回列标题。
Say for example this test dataframe:
比如说这个测试数据框:
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
And I need to locate the column name for e.g. where value = 38.15. What is the best way of doing so?
我需要找到列名,例如值= 38.15。这样做的最佳方法是什么?
Thanks
谢谢
回答by EdChum
Seeing as you only have a single row then you can call iloc[0]
on the result and use this to mask the columns:
看到您只有一行,那么您可以调用iloc[0]
结果并使用它来屏蔽列:
In [47]:
df.columns[(df == 38.15).iloc[0]]
Out[47]:
Index(['col7'], dtype='object')
Breaking down the above:
分解上述内容:
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
You can also use idxmax
with param axis=1
:
您还可以idxmax
与 param 一起使用axis=1
:
In [52]:
(df == 38.15).idxmax(axis=1)[0]
Out[52]:
'col7'
回答by ysearka
You can use data frame slicing and then get the columns names:
您可以使用数据框切片,然后获取列名称:
df.ix[:,df.loc[0] == 38.15].columns
output:
输出:
Index([u'col7'], dtype='object')
回答by piRSquared
just for the sake of throwing something a bit different into the ring:
只是为了将一些不同的东西扔进戒指:
row = df.iloc[0]
row.reset_index().set_index(0).loc[38.15]
回答by Aida Haliti
Let's say we have this df. Checking only the first three rows of the dfwe want to get the name of the column where the specific value is 5.
假设我们有这个df。仅检查df 的前三行,我们希望获得特定值为 5 的列的名称。
df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=list('ABCD'))
df.head(3)
We can do this:
我们可以完成这个:
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'