我有两个DataFrame:dfSide0
和dfSide1
,它们具有相同的列,但行数不同。 dfSide0
有17096行,dfSide1
有4961行。我想做的是获取dfSide0['distoperator']
的值之间所有可能的减法中的最小值
和dfSide1['distoperator']
,但仅当条件dfSide0['camera_row'] == dfSide1['camera_row']
为True时。
所以我执行以下操作:
temp = []
for i in dfSide0['distoperator']:
if dfSide0['camera_row'] == dfSide1['camera_row']:
c = min(abs(i - dfSide1['distoperator']))
temp.append(c)
,我收到以下错误消息:
---------------------------------------------------------------------------
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
我该怎么办?
答案 0 :(得分:0)
感谢您的快速响应。 为了使事情更清楚: dfSide0和dfSide1都有两列:'distance'是浮点数,'camera_row'是具有值'a','b','c','d','e'和'f'的字符串 dfSide0具有17096行,dfSide1具有4961行。 我需要减去两个数据帧之间的“距离”列中的所有值,并找到最小值,但前提是列“ camera_row”中的值相同。到目前为止,确切的代码如下:
temp = []
for i in dfSide0['distance']:
c = min(abs(i - dfSide1['distance']))
temp.append(c)
它可以工作,但是我还没有找到一种仅在
时才准确地为for循环执行此操作的方法 dfSide0['camera_row'] == dfSide1['camera_row']
答案 1 :(得分:0)
如果我这样做:
temp = []
for i in dfSide0['distoperator']:
if dfSide0['camera_row'] == dfSide1['camera_row']:
c = min(abs(i - dfSide1['distoperator']))
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
没有if语句,它运行正常。
答案 2 :(得分:0)
我想我找到了答案:
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)
似乎可行!