python - 数据重组问题
问题描述
我正在分块 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
,我缺少一些在幕后构建的简单数据杂技。任何优雅的解决方案肯定会受到赞赏。
解决方案
推荐阅读
- javascript - 将复选框的值从 PHP 中嵌入的 HTML 传递给 Javascript
- android - 转换数组的问题
到可观察的 > 使用 RxJava2 - azure - Redis 缓存 - 何时可以触发大型驱逐?
- python - Discord 机器人问题“是缺少的必需参数”
- java - 循环 JUnit 测试的最佳实践
- python - 如何在python中单击鼠标开始循环
- graphql - 使用 GraphQL + 石墨烯进行删除操作的最佳方法
- ruby-on-rails - 不允许的参数::utf8, :authenticity_token - Rails 5.2 form_with
- jquery - JQuery onclick获取元素id,使用值作为变量来改变css背景颜色的目标类
- javascript - 移动 HTML 表格的列