Python Pandas-将子字符串从一列复制到另一列

时间:2018-06-22 10:33:10

标签: python python-3.x pandas

我有以下数据框

<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-baqh{text-align:center;vertical-align:top}
.tg .tg-9hbo{font-weight:bold;vertical-align:top}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg">
  <tr>
    <th class="tg-9hbo">JobID</th>
    <th class="tg-9hbo">JobName</th>
    <th class="tg-9hbo">JobLink</th>
  </tr>
  <tr>
    <td class="tg-baqh">n/a</td>
    <td class="tg-baqh">Some job name</td>
    <td class="tg-yw4l">https://www.particulawebsite.com/jobs-some-job-name.id123465789</td>
  </tr>
  <tr>
    <td class="tg-baqh">n/a</td>
    <td class="tg-baqh">another job name</td>
    <td class="tg-yw4l">https://www.particulawebsite.com/jobs-another-job-name.id987654321</td>
  </tr>
  <tr>
    <td class="tg-baqh">n/a</td>
    <td class="tg-baqh">yet another name</td>
    <td class="tg-yw4l">https://www.particulawebsite.com/jobs-yet-another-job-name.id987321654</td>
  </tr>
</table>

我要做的是从JobLink列中复制id部分,并将其放在JobID中。到目前为止,尚未成功完成此操作,因此是我的问题。

非常感谢, D

2 个答案:

答案 0 :(得分:0)

希望这会有所帮助!

df['JobID'] = df['JobLink'].map(lambda x: x.split('.')[-1])

这会将“ JobID”的第一行存储为“ id123465789”

答案 1 :(得分:0)

这样的事情怎么样?

df.JobID = df.JobLink.str.extract(r'\.id(.+)$')

输出:

0      JobID           JobName                                            JobLink
1  123465789     Some job name  https://www.particulawebsite.com/jobs-some-job...
2  987654321  another job name  https://www.particulawebsite.com/jobs-another-...
3  987321654  yet another name  https://www.particulawebsite.com/jobs-yet-anot...
相关问题