从两列数据中创建pandas中的新列

时间:2018-03-15 10:08:01

标签: python pandas

这是pandas文件的头部

In [19]: train.head()
Out[19]: 
   Unnamed: 0                id  \
0           1  0a58358a2afd3e4e   
1           2  6b2bb500b6a38aa0   
2           3  b399f09dee9c3c67   
3           4  19ace29d77a5be66   
4           5  2c9c54b62f0a6a37   
                                                 url  landmark_id  exists  \
0  http://lh6.ggpht.com/-igpT6wu0mIA/ROV8HnUuABI/...         6651    True   
1  http://lh6.ggpht.com/-vKr5G5MEusk/SR6r6SJi6mI/...        11284    True   
2  https://lh3.googleusercontent.com/-LOW2cjAqubA...         8429    True   
3  https://lh5.googleusercontent.com/-tnmSXwQcWL8...         6231    True   
4  https://lh5.googleusercontent.com/-mEaSECO7D-4...        10400    True   
                               filename  
0  training_images/0a58358a2afd3e4e.jpg  
1  training_images/6b2bb500b6a38aa0.jpg  
2  training_images/b399f09dee9c3c67.jpg  
3  training_images/19ace29d77a5be66.jpg  
4  training_images/2c9c54b62f0a6a37.jpg  

我要创建的是一个新的文件名行,其中包含landmark_id,因此我可以使用它来移动文件

In [23]: train['new_filename'] = 'training_images/' + train['landmark_id'] + '/' + train['id'] + '.jpg'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-94fcfab9d25c> in <module>()
----> 1 train['new_filename'] = 'training_images/' + train['landmark_id'] + '/' + train['id'] + '.jpg'
/home/muiruri_samuel/.local/lib/python2.7/site-packages/pandas/core/ops.pyc in wrapper(left, right, name, na_op)
    737                 lvalues = lvalues.values
    738 
--> 739         result = wrap_results(safe_na_op(lvalues, rvalues))
    740         return construct_result(
    741             left,
/home/muiruri_samuel/.local/lib/python2.7/site-packages/pandas/core/ops.pyc in safe_na_op(lvalues, rvalues)
    698         try:
    699             with np.errstate(all='ignore'):
--> 700                 return na_op(lvalues, rvalues)
    701         except Exception:
    702             if isinstance(rvalues, ABCSeries):
/home/muiruri_samuel/.local/lib/python2.7/site-packages/pandas/core/ops.pyc in na_op(x, y)
    684                 result = np.empty(len(x), dtype=x.dtype)
    685                 mask = notna(x)
--> 686                 result[mask] = op(x[mask], y)
    687             else:
    688                 raise TypeError("{typ} cannot perform the operation "
/home/muiruri_samuel/.local/lib/python2.7/site-packages/pandas/core/ops.pyc in <lambda>(x, y)
     76         add=arith_method(operator.add, names('add'), op('+'),
     77                          default_axis=default_axis),
---> 78         radd=arith_method(lambda x, y: y + x, names('radd'), op('+'),
     79                           default_axis=default_axis),
     80         sub=arith_method(operator.sub, names('sub'), op('-'),
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21')

连接错误,出错。

0 个答案:

没有答案