熊猫的人:非独特的群体

时间:2017-04-01 14:36:02

标签: python python-2.7 pandas

我有一个数据框:

Time    c_1     c_2   
 t1      x       1
 t2      x       2
 t3      y       1
 t4      y       2
 t5      1       x
 t6      2       x
 t7      1       y
 t8      2       y

我需要在没有循环的情况下形成2列,例如:

  • new_1:c_1.value出现在c_2中的下一个最早时间(例如,对于t1,new_1 = t5,因为c_1值是' x',下一次' x'出现在c_2是t5)
  • new_2:c_1.value出现在c_1中的下一个最早时间(例如,对于t1,new_1 = t5,因为c_2值为' 1',下一次' 1'出现在c_1是t3)

因此对于上述输入,输出应为:

Time    c_1     c_2    new_1     new_2
 t1      x       1       t5        t5
 t2      x       2       t5        t6
 t3      y       1       t7        t5            
 t4      y       2       t7        t6      
 t5      1       x       NaT       NaT
 t6      2       x       NaT       NaT
 t7      1       y       NaT       NaT
 t8      2       y       NaT       NaT

你会怎么做?

1 个答案:

答案 0 :(得分:1)

这是一个使用apply()和lambda函数从每个行的原始DataFrame中选择正确数据的解决方案。

import pandas as pd

data = {'Time': pd.date_range('1/1/2000', periods=16, freq='D'),
        'c_1': ['x', 'x', 'y', 'y', '1', '2', '1', '2']*2,
        'c_2': ['1', '2', '1', '2', 'x', 'x', 'y', 'y']*2 }

df = pd.DataFrame(data)    
df['new_1'] = df.apply(lambda r: (df.Time[(df.Time>r.Time) & (df.c_2 == r.c_1)].head(1).reset_index(drop=True)), axis=1)
df['new_2'] = df.apply(lambda r: (df.Time[(df.Time>r.Time) & (df.c_1 == r.c_2)].head(1).reset_index(drop=True)), axis=1)
print(df)

输出结果为:

         Time c_1 c_2      new_1      new_2
0  2000-01-01   x   1 2000-01-05 2000-01-05
1  2000-01-02   x   2 2000-01-05 2000-01-06
2  2000-01-03   y   1 2000-01-07 2000-01-05
3  2000-01-04   y   2 2000-01-07 2000-01-06
4  2000-01-05   1   x 2000-01-09 2000-01-09
5  2000-01-06   2   x 2000-01-10 2000-01-09
6  2000-01-07   1   y 2000-01-09 2000-01-11
7  2000-01-08   2   y 2000-01-10 2000-01-11
8  2000-01-09   x   1 2000-01-13 2000-01-13
9  2000-01-10   x   2 2000-01-13 2000-01-14
10 2000-01-11   y   1 2000-01-15 2000-01-13
11 2000-01-12   y   2 2000-01-15 2000-01-14
12 2000-01-13   1   x        NaT        NaT
13 2000-01-14   2   x        NaT        NaT
14 2000-01-15   1   y        NaT        NaT
15 2000-01-16   2   y        NaT        NaT

apply使用axis=1完成,因此它一次迭代一行。 lambda函数仅选择在当前行之后出现且在列中具有正确值的数据帧的行。可能有多个行符合这些条件。 head(1)选择第一个匹配,reset_index(drop=True)确保返回的每个系列具有相同的索引(0),以便apply()将它们全部从返回值放入单个列中。 / p>

相关问题