【发布时间】:2020-03-20 18:25:35
【问题描述】:
我有一个包含一列的数据框。
Index | column1 |
0 and
1 too
2 ask
3 the
4 but
5 hat
6 hot
7 top
8 tap
我想根据条件组合索引之间的行。例如,如果一行有字母“a”,则索引将是:
0, 2, 5, 8
因此,合并行:
(0, 1), (2, 3, 4), (5, 6, 7), (8)
最后的输出是:
Index | column1 |
0 and, too
1 ask, the, but
2 hat, hot, top
3 tap
我试过的是:
[i for i in range(len(df['column1'])) if 'a' in df['column1'][i]]
给我索引:
[0, 2, 5, 8]
但从这里卡住了。谢谢
【问题讨论】:
标签: python pandas list list-comprehension