简体   繁体   English

如何使用 pandas 比较同一行中多列的单列值?

[英]How to compare a value of a single column over multiple columns in the same row using pandas?

I have a dataframe that looks like this:我有一个看起来像这样的 dataframe:

np.random.seed(21) 
df = pd.DataFrame(np.random.randn(8, 4), columns=['A', 'B1', 'B2', 'B3'])
df['current_State'] = [df['B1'][0], df['B1'][1], df['B2'][2], df['B2'][3], df['B3'][4], df['B3'][5], df['B1'][6], df['B2'][7]]
df 

点击这里查看df

I need to create a new column that contains the name of the column where the value of 'current_State' is the same, this is the desired output:我需要创建一个新列,其中包含“current_State”值相同的列名称,这是所需的 output:

期望的输出

I tried many combinations of apply and lambda functions but without success.我尝试了许多 apply 和 lambda 函数的组合,但没有成功。 Any help is very welcome!非常欢迎任何帮助!

You can compare the current_State column with all the remaining columns to create a boolean mask, then use idxmax along axis=1 on this mask to get the name of the column where the value in the given row equal to corresponding value in current_State :您可以将current_State列与所有剩余列进行比较以创建 boolean 掩码,然后在此mask上沿axis=1使用idxmax以获取给定行中的值等于current_State中相应值的列的名称:

c = 'current_State'
df['new_column'] = df.drop(c, 1).eq(df[c], axis=0).idxmax(1)

In case if there is a possibility that there are no matching values we can instead use:如果有可能没有匹配的值,我们可以改用:

c = 'current_State'
m = df.drop(c, 1).eq(df[c], axis=0)
df['new_column'] = m.idxmax(1).mask(~m.any(1))

>>> df

          A        B1        B2        B3  current_State new_column
0 -0.051964 -0.111196  1.041797 -1.256739      -0.111196         B1
1  0.745388 -1.711054 -0.205864 -0.234571      -1.711054         B1
2  1.128144 -0.012626 -0.613200  1.373688      -0.613200         B2
3  1.610992 -0.689228  0.691924 -0.448116       0.691924         B2
4  0.162342  0.257229 -1.275456  0.064004       0.064004         B3
5 -1.061857 -0.989368 -0.457723 -1.984182      -1.984182         B3
6 -1.476442  0.231803  0.644159  0.852123       0.231803         B1
7 -0.464019  0.697177  1.567882  1.178556       1.567882         B2

暂无
暂无

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

相关问题 使用 Python Pandas 将列值与不同列进行比较,并从同一行但不同列返回值 - Compare a Column value to different columns and return a value from same row but different column using Python Pandas 在 Pandas 中,是否有一种方法可以简洁地将多列与每行单列的值进行比较,而无需使用循环? - In Pandas, is there a means to succinctly compare multiple columns to the value of a single column per row without resorting to loops? 如何使用UDF合并单列中的多列并从pyspark中的列中删除0值行 - how to merge the multiple columns in single columns using UDF and remove the 0 value row from the column in pyspark pandas:如何检查列值是否在同一行的其他列中 - pandas: how to check if a column value is in other columns in the same row 如何迭代 Pandas 中的列值并根据同一行中多列的值创建新的观察? - How can I iterate over column values in Pandas and create a new observation based on the values of multiple columns in the same row? 如何使用熊猫在单行中转置多列的单元格? - How to transpose multiple columns' cell in a single row using pandas? Python 如何使用多个 pandas dataframe 列中的值作为元组键和单个列作为值来创建字典 - Python how to create a dictionary using the values in multiple pandas dataframe columns as tuple keys and a single column as value 使用 for 循环将一行(熊猫)与下一行进行比较,如果不同,则从列中获取值 - Compare a row (pandas) with the next row using for loop, and if not the same get a value from a column 如何使用 pandas 将多行转换为同一 ID 的单行 - how to convert multiple rows into single row for same id using pandas 如何在 pandas 中使用 id 将多行合并为一行多列(将具有相同 id 的多条记录聚集到一条记录中) - How to combine multiple rows into a single row with many columns in pandas using an id (clustering multiple records with same id into one record)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM