1

我有一个数据框

id status
1  owner
1  retail
1  shop
1  customer
2  owner
2  retail

我创建了 Last status 的新列,例如

id status   Last status
1  owner     NA
1  retail    owner
1  shop      retail
1  customer  shop
2  owner     NA
2  retail    owner

但我想创建一个流列,即用下一个值附加最后一个状态,比如

id status   From To
1  owner     NA
1  retail    owner -> retail
1  shop      retail -> shop
1  customer  shop -> customer
2  owner     NA
2  retail    owner -> retail

非常感谢提前

4

2 回答 2

0

尝试:

df['From to'] = df['last status'] + '->' + df['status']

于 2020-10-18T06:30:10.087 回答
0

让我们做groupby+shift并与status列连接:

df['From to'] = df.groupby('id')['status'].shift() + ' -> ' + df['status']

   id    status           From to
0   1     owner               NaN
1   1    retail   owner -> retail
2   1      shop    retail -> shop
3   1  customer  shop -> customer
4   2     owner               NaN
5   2    retail   owner -> retail
于 2020-10-18T06:32:03.503 回答