[英]Appending list with highest value from two columns using for loop in pandas
I have two columns: column A, and column B.我有两列:A 列和 B 列。
I would like to find whether the value in each row of column A is larger than the value for the same row in column B, and if it is append a list with these values.我想找出 A 列每一行的值是否大于 B 列同一行的值,如果是 append 则列出这些值。
I'm able to append the list if the value in column A is higher than a set value, but I'm unsure how to compare it to the value from column B.如果 A 列中的值高于设定值,我可以 append 列表,但我不确定如何将其与 B 列中的值进行比较。
The below code appends the list if the value in column A is higher than 4. Hopefully I'm on the right track and can just substitute 4 with some other code?如果 A 列中的值高于 4,则下面的代码会附加列表。希望我走在正确的轨道上,可以用其他代码替换 4 吗?
list = []
for x in A:
if x > 4:
list.append(x)
print(list)
Any help would be greatly appreciated.任何帮助将不胜感激。
Thank you!谢谢!
An approach could be:一种方法可以是:
import pandas as pd
df = pd.DataFrame({"A":[2, 3, 4, 5], "B":[1, 4, 6, 3]}) # Test DataFrame
print(list(df[df["A"] > df["B"]]["A"]))
OUTPUT OUTPUT
[2, 5]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.