Pandas系列记录数字变化

2024-04-25 14:57:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小组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

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


Tags: to数据方法标记元素类型dataframe时间
1条回答
网友
1楼 · 发布于 2024-04-25 14:57:23

我做了一些组合,先把这些柱子叠起来,然后沿着它们旋转。你知道吗

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

相关问题 更多 >