pandas 如何为每个循环遍历数据框中的两列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42542702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to for each loop through two columns in a dataframe?
提问by Luciano
I have a dataframe containing 7 columns and I want to simultaneously loop through two of them to compare the values in each row. This is my for loop header, where watchCol and diaryCol are column numbers:
我有一个包含 7 列的数据框,我想同时循环遍历其中的两个以比较每行中的值。这是我的 for 循环标题,其中 watchCol 和 diaryCol 是列号:
for watch, diary in df.iloc[:, watchCol], df.iloc[:, diaryCol]:
When I run this, I get the following error on that line:
当我运行它时,我在该行上收到以下错误:
ValueError: too many values to unpack (expected 2)
ValueError:解包的值太多(预期为 2)
What am I doing wrong?
我究竟做错了什么?
Thanks
谢谢
EDIT:
编辑:
Both columns contain datetimes. I need to compare the two values, and if the difference is within a certain range, I copy the value from the watchCol into another column, otherwise I move to the next row.
两列都包含日期时间。我需要比较这两个值,如果差异在一定范围内,我将值从 watchCol 复制到另一列,否则我移动到下一行。
采纳答案by dataflow
If you're trying to compare entries row by row, try this:
如果您尝试逐行比较条目,请尝试以下操作:
import pandas as pd
df = pd.DataFrame({"a": [2, 2, 2, 2, 2], "b": [4, 3, 2, 1, 0]})
df["a greater than b"] = df.apply(lambda x: x.a > x.b, axis=1)
print df
a b a greater than b
0 2 4 False
1 2 3 False
2 2 2 False
3 2 1 True
4 2 0 True
That said, if you did want to loop through the elements row by row:
也就是说,如果您确实想逐行遍历元素:
for a, b in zip(df.iloc[:, 0], df.iloc[:, 1]):
print a, b
2 4
2 3
2 2
2 1
2 0