熊猫系列 - 记录数值变化

时间:2014-08-18 15:00:11

标签: python pandas

我有一个小组dataframe,对10年内个人的位置数据进行了大量观察。它看起来像这样:

     personid     location_1991   location_1992  location_1993  location_1994 
0    111          1               1             2              2 
1    233          3               3             4              999  
2    332          1               3             3               3 
3    454          2               2             2               2             
4    567          2               1             1               1

我想通过为每种类型的过渡创建变量来跟踪每个人的过渡。每当一个人过渡到每个位置类型时,我都想要一个标记列。理想情况下,这看起来像:

     personid     transition_to_1    transition_to_2   transition_to_3   transition_to_4       
0    111          0                  1                 0                 0 
1    233          0                  0                 0                 1  
2    332          0                  0                 1                 0 
3    454          0                  0                 0                 0             
4    567          1                  0                 0                 0

到目前为止,我已经尝试遍历每一行,然后遍历行中的每个元素以检查它是否与前一行相同。这似乎是时间密集的。有没有更好的方法来跟踪数据框每行中值的变化?

1 个答案:

答案 0 :(得分:2)

我做了一些组合,首先堆叠这些列,然后沿着它们旋转。

df = pd.DataFrame(pd.read_clipboard())
df2 = pd.DataFrame(df.set_index('personid').stack(), columns=['location'])
df2.reset_index(inplace=True)
df2.reset_index(inplace=True)
df3 = df2.pivot(index='index', columns='location', values='personid')
df3 = df3.fillna(0)

到目前为止,它看起来像这样:

location  1    2    3    4    999
index                            
0         111    0    0    0    0
1         111    0    0    0    0
2           0  111    0    0    0
3           0  111    0    0    0
4           0    0  233    0    0
5           0    0  233    0    0
6           0    0    0  233    0
7           0    0    0    0  233
8         332    0    0    0    0
9           0    0  332    0    0
10          0    0  332    0    0
11          0    0  332    0    0
12          0  454    0    0    0
13          0  454    0    0    0
14          0  454    0    0    0
15          0  454    0    0    0
16          0  567    0    0    0
17        567    0    0    0    0
18        567    0    0    0    0
19        567    0    0    0    0

df3['personid'] = df3.max(axis=0, skipna=True)
df3 = df3.set_index('personid', drop=True)
df3[df3 > 0] = 1

它就是这样:

location  1    2    3    4    999
personid                         
111         1    0    0    0    0
567         1    0    0    0    0
567         0    1    0    0    0
332         0    1    0    0    0
233         0    0    1    0    0
233         0    0    1    0    0
233         0    0    0    1    0
233         0    0    0    0    1
332         1    0    0    0    0
332         0    0    1    0    0
332         0    0    1    0    0
332         0    0    1    0    0
454         0    1    0    0    0
454         0    1    0    0    0
454         0    1    0    0    0
454         0    1    0    0    0
567         0    1    0    0    0
567         1    0    0    0    0
567         1    0    0    0    0
567         1    0    0    0    0