我在python中编写了一个函数,以便使用数据框'File2'的每一行中的信息对数据框'Demand'进行一些实验:
def Compute(row):
File = pd.concat([File2[ (File2.Number == row['Number']) ]]*len(Demand), ignore_index = True)
File.Number = Demand.Number
result = pd.merge(Demand, File, on = 'Number')
result['Situation'] = 0
result.Situation = result.apply(lambda r: 1 if (r.Arrival <= r.Time2) & (r.Departure > r.Time2) & (r.Scenario == r.Scenario2) & (sum(pd.Series(r.Station).isin([row['Station2']])) != 0) else 0, axis = 1)
if len(result) != 0:
result = result[ (result.Situation == 1) ]
return result
File2.apply(Compute, axis = 1)
虽然我在'File2'上使用简单的for循环没有任何错误,但是当我使用这个函数时出现以下错误:
Traceback (most recent call last):
File "<ipython-input-133-cbdaa2bf5936>", line 1, in <module>
File2.apply(Compute, axis = 1)
File "C:\Users\Research\Anaconda3\lib\site-packages\pandas\core\frame.py", line 3972, in apply
return self._apply_standard(f, axis, reduce=reduce)
File "C:\Users\Research\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4081, in _apply_standard
result = self._constructor(data=results, index=index)
File "C:\Users\Research\Anaconda3\lib\site-packages\pandas\core\frame.py", line 226, in __init__
mgr = self._init_dict(data, index, columns, dtype=dtype)
File "C:\Users\Research\Anaconda3\lib\site-packages\pandas\core\frame.py", line 363, in _init_dict
dtype=dtype)
File "C:\Users\Research\Anaconda3\lib\site-packages\pandas\core\frame.py", line 5163, in _arrays_to_mgr
arrays = _homogenize(arrays, index, dtype)
File "C:\Users\Research\Anaconda3\lib\site-packages\pandas\core\frame.py", line 5477, in _homogenize
raise_cast_failure=False)
File "C:\Users\Research\Anaconda3\lib\site-packages\pandas\core\series.py", line 2887, in _sanitize_array
subarr = _asarray_tuplesafe(data, dtype=dtype)
File "C:\Users\Research\Anaconda3\lib\site-packages\pandas\core\common.py", line 2011, in _asarray_tuplesafe
result[:] = [tuple(x) for x in values]
ValueError: cannot copy sequence with size 11 to array axis with dimension 0
"cannot copy sequence with size 11 to array axis with dimension 0"
你能告诉我潜在的问题吗?