[英]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 ![]() |
from![]() |
to![]() |
text![]() |
---|---|---|---|
1 ![]() |
max![]() |
hi![]() |
|
1 ![]() |
charls![]() |
max![]() |
hey !![]() |
1 ![]() |
alex![]() |
max![]() |
howdi !![]() |
2 ![]() |
sandy![]() |
hi![]() |
|
2 ![]() |
sandy![]() |
hey??!![]() |
|
2 ![]() |
sam![]() |
sandy![]() |
jeah?![]() |
I want only the text values and compress each three rows into one / drop the other two rows.我只想要文本值并将每三行压缩成一行/删除另外两行。
id ![]() |
author_text_1 ![]() |
author_text_2 ![]() |
author_text_3 ![]() |
reply_text_1![]() |
reply_text_2![]() |
---|---|---|---|---|---|
1 ![]() |
hi![]() |
hey !![]() |
howdi !![]() |
||
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.