【发布时间】:2021-07-22 01:17:03
【问题描述】:
有没有办法在每列的数据框列表中拆分数据框,然后在列名称中添加计数器或其他内容,例如ab_a, cd_a
一些随机数据作为示例
data = pd.DataFrame({'ab': [1, 3, 1, 4, -1, 1],
'cd': [1, 1, -0, 1, -0],
'ef': [1, 2, 1, 1, 2],
'gh': [1, 4, 2, 3, 1]})
预期的输出类似于
lst = [ab \
1
2
1
4
-1
1,
cd \
1
1
-0
1
-0,
ef\
1
2
1
1
2,
gh\
1
4
2
3
1]
您可以在其中访问列表中的每个元素,例如lst[0] - 将列 ab 作为单个数据框输出
**ab**
1
2
1
4
-1
1
谢谢!
【问题讨论】:
-
你能编辑你的帖子并把预期的输出放在那里吗?
-
@AndrejKesely 确定 - 希望这不会让它更加混乱......感谢您的关注!
-
@AndrejKesely 这会引发以下错误:AttributeError: 'DataFrame' object has no attribute 'to_frame'
-
你的变量
data是一个DataFrame吗?您可以尝试删除.to_frame() -
@AndrejKesely 是的,我后来想通了!非常感谢:D
标签: python pandas list loops subset