我正在分块 a DataFrame,然后想在块上粘贴一ID列(填充 pos 值)并DataFrame从堆叠的块中构建一个新的。我正在尝试这个...
def organizer(org_seq, size, overlap):
new_seq = pd.DataFrame(columns=list(org_seq.columns.values))
for pos in range(0, len(org_seq) - size, size-overlap):
seq_holder = org_seq.iloc[pos:pos + size]
seq_holder.insert(0, 'ID', pos)
new_seq.append(seq_holder, ignore_index=True)
return new_seq
编辑此修改产生了正确的行为,但是它很费力
new_seq = new_seq.append(seq_holder, ignore_index=True)
结束编辑
我从这个函数中得到一个空的 DataFrame。我想要的是这里的例子:
数据框在:
0 a b
1 a b
2 a b
3 a b
4 a b
5 a b
新DataFrame出:
0 a b pos-id
1 a b pos-id
2 a b pos-id
3 a b pos-id
1 a b pos-id
2 a b pos-id
3 a b pos-id
4 a b pos-id
2 a b pos-id
3 a b pos-id
4 a b pos-id
5 a b pos-id
我敢打赌DataFrame,我缺少一些在幕后构建的简单数据杂技。任何优雅的解决方案肯定会受到赞赏。