简体   繁体   English

从 pandas df 中的下一行获取值并插入到上一行

[英]get values from next row in pandas df and insert to previous row

I have a large q/a dataset, which always contains 3 rows per unique id /they are already grouped.我有一个大型 q/a 数据集,每个唯一 ID 始终包含 3 行/它们已经分组。

id ID from to text文本
1 1 max最大限度 hi你好
1 1 charls查尔斯 max最大限度 hey !嘿 !
1 1 alex亚历克斯 max最大限度 howdi !你好!
2 2 sandy hi你好
2 2 sandy hey??!嘿??!
2 2 sam山姆 sandy jeah?耶?

I want only the text values and compress each three rows into one / drop the other two rows.我只想要文本值并将每三行压缩成一行/删除另外两行。

id ID author_text_1 author_text_1 author_text_2 author_text_2 author_text_3 author_text_3 reply_text_1回复文本_1 reply_text_2回复文本_2
1 1 hi你好 hey !嘿 ! howdi !你好!
2 2 hi你好 hey??!嘿??! jeah?耶?

I tried for each group of 3 rows, to drop the last two rows and add the values from each 'text' column to the corresponding 'author_text' and 'reply_text' columns.我尝试为每组 3 行删除最后两行并将每个“文本”列中的值添加到相应的“作者文本”和“回复文本”列。

I am struggling with implementing the case that the author could make the question post and 2 more posts which would fill the three rows.我正在努力实现作者可以发布问题帖子和另外 2 个帖子来填充三行的案例。

You need to create the column as the new key for pivot您需要创建该列作为pivot的新键

df['key'] = np.where(df.to.isna(),'author_text_','reply_text_')
df['key'] += df.groupby(['id','key']).cumcount().add(1).astype(str)
out = df.pivot('id','key','text')
Out[254]: 
key author_text_1 author_text_2 reply_text_1 reply_text_2
id                                                       
1          NaN hi           NaN         hey!       howdi!
2              hi        hey!?!        jeah?          NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM