python中的条件for循环

python中的条件for循环,python,dataframe,for-loop,if-statement,Python,Dataframe,For Loop,If Statement,我有两个数据帧:dfSide0和dfSide1,它们具有相同的列,但行数不同dfSide0有17096行,而dfSide1有4961行。我想做的是得到dfSide0['distoperator'] 和dfSide1['distoperator']但仅当条件dfSide0['camera\u row']==dfSide1['camera\u row']为真时 因此,我做了以下工作: temp = [] for i in dfSide0['distoperator']: if dfSide0

我有两个数据帧:
dfSide0
dfSide1
,它们具有相同的列,但行数不同
dfSide0
有17096行,而
dfSide1
有4961行。我想做的是得到
dfSide0['distoperator']
dfSide1['distoperator']
但仅当条件
dfSide0['camera\u row']==dfSide1['camera\u row']
为真时

因此,我做了以下工作:

temp = []
for i in dfSide0['distoperator']:
    if dfSide0['camera_row'] == dfSide1['camera_row']:
        c = min(abs(i - dfSide1['distoperator']))
        temp.append(c)
temp = []
for i in dfSide0['distance']:
    c = min(abs(i - dfSide1['distance']))
    temp.append(c)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-222-625fcefe40b7> in <module>()
      1 temp = []
      2 for i in dfSide0['distoperator']:
----> 3     if dfSide0['camera_row'] == dfSide1['camera_row']:
      4         c = min(abs(i - dfSide1['distoperator']))
      5         temp.append(c)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
   1188 
   1189         elif isinstance(other, ABCSeries) and not self._indexed_same(other):
-> 1190             raise ValueError("Can only compare identically-labeled "
   1191                              "Series objects")
   1192 

ValueError: Can only compare identically-labeled Series objects
我得到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-225-625fcefe40b7> in <module>()
      1 temp = []
      2 for i in dfSide0['distoperator']:
----> 3     if dfSide0['camera_row'] == dfSide1['camera_row']:
      4         c = min(abs(i - dfSide1['distoperator']))
      5         temp.append(c)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
   1188 
   1189         elif isinstance(other, ABCSeries) and not self._indexed_same(other):
-> 1190             raise ValueError("Can only compare identically-labeled "
   1191                              "Series objects")
   1192 

ValueError: Can only compare identically-labeled Series objects
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
1温度=[]
DfsId0['distoperator']中的i为2:
---->3如果dfSide0['camera\u row']==dfSide1['camera\u row']:
4 c=最小值(abs(i-dfSide1['distoperator']))
5临时附加(c)
包装器中的C:\ProgramData\Anaconda3\lib\site packages\pandas\core\ops.py(self、other、axis)
1188
1189 elif isinstance(其他,ABC系列)和非自索引(其他):
->1190 raise VALUE ERROR(“只能比较标签相同的项目”
1191“系列对象”)
1192
ValueError:只能比较标签相同的系列对象

我该怎么办?

谢谢您的快速响应。 为了让事情更清楚: dfSide0和dfSide1都有两列:“距离”是一个浮点数,“相机行”是一个字符串,值为“a”、“b”、“c”、“d”、“e”和“f” dfSide0有17096行,dfSide1有4961行。 我需要减去两个数据帧之间“距离”列中的所有值并找到最小值,但只有当“camera_row”列中的值相同时才需要。到目前为止,确切的代码如下:

temp = []
for i in dfSide0['distoperator']:
    if dfSide0['camera_row'] == dfSide1['camera_row']:
        c = min(abs(i - dfSide1['distoperator']))
        temp.append(c)
temp = []
for i in dfSide0['distance']:
    c = min(abs(i - dfSide1['distance']))
    temp.append(c)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-222-625fcefe40b7> in <module>()
      1 temp = []
      2 for i in dfSide0['distoperator']:
----> 3     if dfSide0['camera_row'] == dfSide1['camera_row']:
      4         c = min(abs(i - dfSide1['distoperator']))
      5         temp.append(c)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
   1188 
   1189         elif isinstance(other, ABCSeries) and not self._indexed_same(other):
-> 1190             raise ValueError("Can only compare identically-labeled "
   1191                              "Series objects")
   1192 

ValueError: Can only compare identically-labeled Series objects
它是有效的,但我还没有找到一种方法来准确地为循环执行此操作,只有在

dfSide0['camera\u row']==dfSide1['camera\u row']
如果我这样做:

temp = []
for i in dfSide0['distoperator']:
    if dfSide0['camera_row'] == dfSide1['camera_row']:
        c = min(abs(i - dfSide1['distoperator']))
        temp.append(c)
我得到以下信息:

temp = []
for i in dfSide0['distoperator']:
    if dfSide0['camera_row'] == dfSide1['camera_row']:
        c = min(abs(i - dfSide1['distoperator']))
        temp.append(c)
temp = []
for i in dfSide0['distance']:
    c = min(abs(i - dfSide1['distance']))
    temp.append(c)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-222-625fcefe40b7> in <module>()
      1 temp = []
      2 for i in dfSide0['distoperator']:
----> 3     if dfSide0['camera_row'] == dfSide1['camera_row']:
      4         c = min(abs(i - dfSide1['distoperator']))
      5         temp.append(c)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
   1188 
   1189         elif isinstance(other, ABCSeries) and not self._indexed_same(other):
-> 1190             raise ValueError("Can only compare identically-labeled "
   1191                              "Series objects")
   1192 

ValueError: Can only compare identically-labeled Series objects
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
1温度=[]
DfsId0['distoperator']中的i为2:
---->3如果dfSide0['camera\u row']==dfSide1['camera\u row']:
4 c=最小值(abs(i-dfSide1['distoperator']))
5临时附加(c)
包装器中的C:\ProgramData\Anaconda3\lib\site packages\pandas\core\ops.py(self、other、axis)
1188
1189 elif isinstance(其他,ABC系列)和非自索引(其他):
->1190 raise VALUE ERROR(“只能比较标签相同的项目”
1191“系列对象”)
1192
ValueError:只能比较标签相同的系列对象

如果没有if语句,它运行良好。

我想我找到了答案:

temp = []
k = -1
for i in dfSide0['distoperator']:
    k = k+1
    j = dfSide0.index[k]
    c = min(abs(i - dfSide1['distoperator'][dfSide1['camera_row'] == dfSide0['camera_row'][j]]))
    temp.append(c)

它似乎起作用了

你有电话吗?否则,下票数将滚滚而来……示例代码中的缩进已关闭,为了进行比较,请使用
=
而不是
=
。除此之外,如果不知道数据的外观,很难提供帮助。如果您有不同的行数,那么如何
dfSide0['columnx']==dfSide1['columnx']
?这里没有逐行比较,但是整个系列……是的,数据帧的行数不同。那么,您建议我如何实现这个条件?==您需要使用==和not=进行比较。如果dfSide0['camera_row']==dfSide1['camera_row']:在for循环之前的行上添加,并适当缩进。