简体   繁体   English

如何找到 dataframe 中两列之间的差异,列中的任一行在 Python 中具有 -ve 和/或 +ve 值

[英]How to find the difference between two columns in a dataframe with either rows in the column have -ve and/or +ve values in Python

I have two columns (x & y) in a df with 4 row scenarios (+ve)-(+ve), (-ve)-(-ve), (+ve)-(-ve) and (-ve)-(+ve).我在 df 中有两列 (x & y),有 4 行场景 (+ve)-(+ve)、(-ve)-(-ve)、(+ve)-(-ve) 和 (-ve) -(+ve)。

df df

 x    y
 3    2
-4   -2
 2   -1
-6    1

I want to insert a third column z in the same df and the end result as shown below我想在同一个df中插入第三列z,最终结果如下所示

df df

 x    y    z
 3    2    1
-4   -2   -2
 2   -1    3
-6    1   -5

Are you looking for this?你在找这个吗?

df['z'] = df['x'] - df['y']
x['z']=x.apply(lambda row: row[0]-row[1],axis=1)

在此处输入图像描述

passing axis=1 to apply() helps us to access all values of a row.将 axis=1 传递给 apply() 可以帮助我们访问一行的所有值。

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

相关问题 Python Dataframe:如果行值为-ve,则减去两列? - Python Dataframe: Subract two columns if row value is -ve? Python如何调用数据帧的两列中的任何一列 - Python how to call either of the two columns of a dataframe 在 DataFrame 中查找在两列中有重复的行 - Find rows in DataFrame that have duplicates in two columns 在Python DataFrame中如何找出具有有效列值的行数 - In Python DataFrame how to find out number of rows that have valid values of columns Python-如何使用CSV文件中的循环查找同一列的两行之间的差异? - Python - How can I find difference between two rows of same column using loop in CSV file? 如何在列的两个值之间选择数据框中的所有行 - How to select all rows in a dataframe between two values of column 如何在Python中找到两个矩阵之间的差异,结果不应该带有减号的任何值 - How to find the difference between two matrices in Python with the result should not have any values with minus sign 如何按两列值之间的行对熊猫数据框进行切片? - How to slice a pandas dataframe by rows between two column values? 如何在 dataframe 中在一列中的两个值之间迭代行? - How to iterate over rows in a dataframe between two values in one column? 如何在 pandas dataframe 中找到 Max_winning_streak(连续 +ve 值的最大数量) - how to find Max_winning_streak (max number of consecutive +ve values) in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM