简体   繁体   English

Python数据框获取一个值的行和列

[英]Python dataframe get the row and column of one value

   b1  b2
0   0   1
1   2   3
2   4   5

For example, we have a dataframe a in python, is that possible to get value (b1,2) when you input 4, and get value (b2,2) when input value 5. 例如,我们在python中有一个数据框a,当您输入4时可以获取值(b1,2),而当输入值5时可以获取值(b2,2)。

Also there's duplicate value in the dataframe, i want to get all their location. 在数据框中也有重复的值,我想获取它们的所有位置。

Option 1 选项1
one-line 一条线
Not recommended! 不建议!

a.unstack().eq(4).compress(lambda x: x).index.to_series().values[0]

('b1', 2)

Option 2 选项2
np.where

i, j = np.where(a == 4)
# or faster with
# i, j = np.where(a.values == 4)
(a.columns[j[0]], a.index[i[0]])

('b1', 2)

Option 3 选项3

s = a.unstack()
pd.Series(s.index.values, s.values).loc[4]

('b1', 2)

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

相关问题 Python数据框获取每行最后一个非空列的值 - Python Dataframe Get Value of Last Non Null Column for Each Row Python:在DataFrame中,在新列中为另一列中具有最高值的行添加值,在第三列中添加相同的字符串 - Python: In DataFrame, add value in a new column for row with highest value in another column and string identical in a third one Dataframe:比较列值和下一行 - Dataframe: compare column value and one row below Python中的逻辑根据数据帧中某列的最大值仅返回相似行中的一行 - Logic in Python to return only one row among similar row(s) based on the maximum value in a column in a dataframe 如果一个数据框的行值在另一数据框的列中,则创建一个新列并获取该索引 - Create a new column if one dataframe's row value is in another data frame's column and get that index Python Dataframe 将列名转换为行值 - Python Dataframe to convert column name as row value Python Pandas Dataframe 按列和行值 - Python Pandas Dataframe By Column and Row Value Python 更改特定列和行中的 Dataframe 值 - Python Change Dataframe Value in Specific Column And Row 获取熊猫数据框中特定行和列的值 - get a value of a specific row and column in pandas dataframe 基于另一个 dataframe 的行值对一个 dataframe 中的列求和 - Sum column in one dataframe based on row value of another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM