我有2个数据帧,我需要使用第二个数据帧中的值将新列添加到第一个数据帧 第一个方向是
ID,"url","used_at","active_seconds"
8075643aab791cec7dc9d18926958b67,"sberbank.ru/ru/person/promo/10mnl?utm_source=Vesti.ru&utm_medium=html&utm_campaign=10_million_users_SBOL_dec2015&utm_term=every14_syncbanners",2016-01-01 00:03:16,183
a04a8041ffa6fe1b85471ca5af1ee575,"online.rsb.ru/hb/faces/system/login/rslogin.jsp?credit=false",2016-01-01 00:04:36,42
a04a8041ffa6fe1b85471ca5af1ee575,"online.rsb.ru/hb/faces/system/login/sms/sms.jsp?smsAuth=true",2016-01-01 00:05:18,22
a04a8041ffa6fe1b85471ca5af1ee575,"online.rsb.ru/hb/faces/rs/RSIndex.jspx",2016-01-01 00:05:40,14
a04a8041ffa6fe1b85471ca5af1ee575,"online.rsb.ru/hb/faces/rs/payments/PaymentReq.jspx",2016-01-01 00:05:54,22
ba880911a6d54f6ea6d3145081a0e0dd,"homecredit.ru/help/quest/feedback.php",2016-01-01 00:06:12,2
第二个测向看起来像
^{pr2}$如果我没有正则表达式,我使用
df1['code'] = df1.url.map(df2.set_index('URL')['Code'])
但我不能这样做,因为df2.URL
是regex。
但是
df1['code'] = df1['url'].replace(df2['URL'], df2['Code'], regex=True)
不起作用。在
根据我的评论,}参数。传递列表反而起作用:
pandas.Series.replace()
方法不允许Series
对象作为to_replace
和{生成以下输出:
^{pr2}$为了回答您的附加注释,您不能在
df1.code
中的df2.Code
在df1.url
与任何regex字符串都不匹配的行中,但是您可以为这些情况提供一个值(例如None
)以将其放入列中。例如,可以通过添加以下行来完成:其中
print df1[['url', 'code']]
返回以下内容:相关问题 更多 >
编程相关推荐