根据其他列中的值创建新列

时间:2018-07-02 20:17:04

标签: python pandas numpy dataframe

我有一个看起来像这样的DataFrame:

import numpy as np
import pandas as pd

df=pd.DataFrame([['vt 40462',5,6],[5,6,6],[5,5,8],[4,3,1],['vl 6450',5,6],[5,6,7],
             [1,2,3],['vt 40462',5,6],[5,5,8],['vl 658',6,7],[5,5,8],[4,3,1],['vt 40461',5,6],[5,5,8],
             [7,8,5]],columns=['A','B','C'])

df

         A  B  C
0 vt 40462  5  6
1        5  6  6
2        5  5  8
3        4  3  1
4  vl 6450  5  6
5        5  6  7
6        1  2  3
7  vt 40462  5  6
8        5  5  8
9   vl 658  6  7
10       5  5  8
11       4  3  1
12 vt 40461 5  6
13        5 5  8
14        7 8  5

我想给索引vtvlA之间的值,并创建一个新列:

         A  B  C  D
0 vt 40462  5  6  vt 40462
1        5  6  6  vt 40462
2        5  5  8  vt 40462
3        4  3  1  vt 40462
4  vl 6450  5  6  vl 6450
5        5  6  7  vl 6450
6        1  2  3  vl 6450
7 vt 40462  5  6  vt 40462
8        5  5  8  vt 40462
9   vl 658  6  7  vl 658
10       5  5  8  vl 658
11       4  3  1  vl 658
12 vt 40461 5  6  vt 40461
13        5 5  8  vt 40461
14        7 8  5  vt 40461

2 个答案:

答案 0 :(得分:3)

使用var items = document.querySelectorAll('#clock'); for (var i = 0, len = items.length; i < len; i++) { (function () { var e = new Date("2018-12-31").getTime(), t = this.querySelector("[data-js=countdown]"), n = this.querySelector("[data-js=countdown-endtext]"), day = this.querySelector("[data-js=countdown-day]"), hour = this.querySelector("[data-js=countdown-hour]"), min = this.querySelector("[data-js=countdown-minute]"), sec = this.querySelector("[data-js=countdown-second]"), s = this.gjs_countdown_interval; s && s && clearInterval(s); var l = function (e, t, n, s) { day.innerHTML = e < 10 ? "0" + e : e, hour.innerHTML = t < 10 ? "0" + t : t, min.innerHTML = n < 10 ? "0" + n : n, sec.innerHTML = s < 10 ? "0" + s : s }, u = function () { var day = (new Date).getTime(), hour = e - day, min = Math.floor(hour / 864e5), sec = Math.floor(hour % 864e5 / 36e5), s = Math.floor(hour % 36e4 / 6e4), u = Math.floor(hour % 6e4 / 1e3); l(min, sec, s, u), hour < 0 && (clearInterval(c), n.innerHTML = "EXPIRED", t.style.display = "none", n.style.display = "") }; if (e) { var c = setInterval(u, 1e3); this.gjs_countdown_interval = c, n.style.display = "none", t.style.display = "", u() } else l(0, 0, 0, 0) }.bind(items[i]))(); } ,如果未找到'',则返回NaN使用<section class="flex-sect"> <div id="clock" class="countdown"> <span data-js="countdown" class="countdown-cont"> <div class="countdown-block"> <div data-js="countdown-day" class="countdown-digit"></div> <div class="countdown-label">days</div> </div> <div class="countdown-block"> <div data-js="countdown-hour" class="countdown-digit"></div> <div class="countdown-label">hours</div> </div> <div class="countdown-block"> <div data-js="countdown-minute" class="countdown-digit"></div> <div class="countdown-label">minutes</div> </div> <div class="countdown-block"> <div data-js="countdown-second" class="countdown-digit"></div> <div class="countdown-label">seconds</div> </div> </span> <span data-js="countdown-endtext" class="countdown-endtext"></span> </div> </section> 来填充NaN并将字段连接在一起并分配给'D':

str.split

输出:

ffill

答案 1 :(得分:3)

另一种方法是将assignD设为以字母开头的A的所有值,然后使用df.ffill()摆脱{{1 }}:

NaN

或多或少等效,但分为2个步骤:

df.assign(D=df.loc[df.A.str.contains('^[A-Za-z]', na=False), 'A']).ffill()


           A  B  C         D
0   vt 40462  5  6  vt 40462
1          5  6  6  vt 40462
2          5  5  8  vt 40462
3          4  3  1  vt 40462
4    vl 6450  5  6   vl 6450
5          5  6  7   vl 6450
6          1  2  3   vl 6450
7   vt 40462  5  6  vt 40462
8          5  5  8  vt 40462
9     vl 658  6  7    vl 658
10         5  5  8    vl 658
11         4  3  1    vl 658
12  vt 40461  5  6  vt 40461
13         5  5  8  vt 40461
14         7  8  5  vt 40461