首页 > 解决方案 > 数据框转换和字符串连接

问题描述

我有一个看起来像这样的熊猫数据框:

index, a, b
0, i, this
1, belong, is 
2, here, right
0, only, one
0, two, another
1, items, example

我正在尝试获取输出,以便将运行索引折叠起来并连接字符串:

index, a, b
0, i belong here, this is right
1, only, one
2, two items, another example

知道如何以干净的方式做到这一点吗?

标签: pythonpandasdataframe

解决方案


0您需要创建以inindex和累积总和开头的组,然后聚合join

#if index is column
df = df.groupby(df['index'].eq(0).cumsum(), as_index=False).agg(' '.join)
#if index is not column
#df = df.groupby((df.index == 0).cumsum(), as_index=False).agg(' '.join)
print (df)
               a                b
0  i belong here   this is  right
1           only              one
2      two items  another example

详情

print (df['index'].eq(0).cumsum())
0    1
1    1
2    1
3    2
4    3
5    3
Name: index, dtype: int32

#print ((df.index == 0).cumsum())
#[1 1 1 2 3 3]

推荐阅读