【发布时间】:2021-09-18 22:12:29
【问题描述】:
有一个像这样的df:
df = pd.DataFrame({'words':['hi', 'this', 'is', 'a', 'sentence', 'this', 'is', 'another', 'sentence'], 'indicator':[1,0,0,0,0,1,0,0,0]})
这给了我:
words indicator
0 hi 1
1 this 0
2 is 0
3 a 0
4 sentence 0
5 this 1
6 is 0
7 another 0
8 sentence 0
现在我想合并列 'words' 的所有值,这些值在指示符中的 '1' 之后,直到下一个 '1' 出现。 这样的结果将是理想的结果:
words indicator counter
0 hi this is a sentence 1 5
1 this is another sentence 1 4
这并不容易解释,这就是我依赖这个例子的原因。 我尝试了 groupby 和 split,但无法找到解决方案。 最后一次尝试是设置某种 df.iterrows(),但我现在想避免这种情况,因为实际的 df 非常大。
提前感谢您的帮助!
【问题讨论】:
标签: python pandas dataframe group-by