![](/img/trans.png)
[英]How to store a particular key value from a list of dictionaries in a data frame
[英]How to get specific column key from data frame given a particular element?
考虑数据帧
c1 c2 c3
0 a 3 4 2
1 b 1 2 7
说我想知道第1行中的哪一列具有元素7。我将如何实现它? 我正在尝试使用熊猫来实现它。
如果要获取索引为1
行中第一个值的列:
a = df.loc[1].eq(7).idxmax()
print (a)
c3
说明:
首先通过loc
选择具有索引的列:
print (df.loc[1])
c1 4
c2 2
c3 7
Name: (1, b), dtype: int64
与7
比较:
print (df.loc[1].eq(7))
c1 False
c2 False
c3 True
Name: (1, b), dtype: bool
对于c3
获得最大值的索引,它意味着第一个True
。
如果row包含多个值(此处为7),并且需要所有匹配的列,请使用boolean indexing
:
a = df.loc[1].eq(7)
a = a.index[a].tolist()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.