简体   繁体   English

Concat与Pandas的两列

[英]Concat two columns with Pandas

Hey I'm trying to combine two columns in Pandas, but for some reason I'm having trouble doing so. 嘿,我试图在熊猫中合并两列,但是由于某种原因,我在这样做时遇到了麻烦。

Here is my data: 这是我的数据:

OrderId OrderDate   UserId  TotalCharges    CommonId    PupId   PickupDate  Month   Year
0   262 1/11/2009   47  $ 50.67 TRQKD   2   1/12/2009   1   2009
1   278 1/20/2009   47  $ 26.60 4HH2S   3   1/20/2009   1   2009
2   294 2/3/2009    47  $ 38.71 3TRDC   2   2/4/2009    2   2009
3   301 2/6/2009    47  $ 53.38 NGAZJ   2   2/9/2009    2   2009
4   302 2/6/2009    47  $ 14.28 FFYHD   2   2/9/2009    2   2009

I want to take the columns "Month" and "Year" so they make a new column which has the format: 我想采用“月份”和“年份”列,以便它们创建一个具有以下格式的新列:

"2009-1"
"2009-1"
"2009-1"
"2009-2"
"2009-2"

When I try this: 当我尝试这个:

df['OrderPeriod'] = df[['Year', 'Month']].apply(lambda x: '-'.join(x), axis=1)

I get this error: 我收到此错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-24-ebbfd07772c4> in <module>()
----> 1 df['OrderPeriod'] = df[['Year', 'Month']].apply(lambda x: '-'.join(x), axis=1)

/Users/robertdefilippi/miniconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
   3970                     if reduce is None:
   3971                         reduce = True
-> 3972                     return self._apply_standard(f, axis, reduce=reduce)
   3973             else:
   3974                 return self._apply_broadcast(f, axis)

/Users/robertdefilippi/miniconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in _apply_standard(self, func, axis, ignore_failures, reduce)
   4062             try:
   4063                 for i, v in enumerate(series_gen):
-> 4064                     results[i] = func(v)
   4065                     keys.append(v.name)
   4066             except Exception as e:

<ipython-input-24-ebbfd07772c4> in <lambda>(x)
----> 1 df['OrderPeriod'] = df[['Year', 'Month']].apply(lambda x: '-'.join(x), axis=1)

TypeError: ('sequence item 0: expected string, numpy.int32 found', u'occurred at index 0')

I'm really at a loss at how to concatenate these two columns. 我真的不知道如何连接这两列。

Help? 救命?

You need to convert the values to string in order to use join. 您需要将值转换为字符串才能使用连接。

df['OrderPeriod'] = df[['Year', 'Month']]\
    .apply(lambda x: '-'.join(str(value) for value in x), axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM