我有一个熊猫数据框,如下所示:
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
有人知道如何以一种干净的方式做到这一点吗?
答案 0 :(得分:3)
您需要创建以0
中的index
开头的组并累积总和,然后汇总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]