【问题标题】:split data frame by all columns and insert in to a list of data frames按所有列拆分数据框并插入到数据框列表中
【发布时间】: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


【解决方案1】:

你可以使用 to_dict(orient='list') -

result = list(df.to_dict(orient='list').values())

输出 -

print(result[1])
[1, 1, 0, 1, 0]

【讨论】:

    【解决方案2】:

    您可以使用.pop,然后使用.to_frame()。例如:

    lst = [data.pop(col).to_frame() for col in data.columns]
    print(lst)
    

    打印:

    [   ab
    0   1
    1   3
    2   1
    3   4
    4  -1,    cd
    0   1
    1   1
    2   0
    3   1
    4   0,    ef
    0   1
    1   2
    2   1
    3   1
    4   2,    gh
    0   1
    1   4
    2   2
    3   3
    4   1]
    

    那么你可以这样做:

    # print first dataframe
    print(lst[0])
    

    打印:

       ab
    0   1
    1   3
    2   1
    3   4
    4  -1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      相关资源
      最近更新 更多