如何基于列行拆分数据框

编程入门 行业动态 更新时间:2024-10-06 09:21:20
本文介绍了如何基于列行拆分数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Excel文件,数据框有20行.在几行之后,又有一个列名行,我想根据列名行划分数据框.

I have one excel file , dataframe have 20 rows . after few rows there is again column names row, i want to divide dataframe based on column names row.

这是示例:

x 0 1 2 3 4 x 23 34 5 6

预期输出为:

df1 x 0 1 2 3 4 df2 x 23 34 5 6

推荐答案

考虑到列名是col,您可以首先使用col所在的col上j1 ...... 0 .... 1..gws-wiz.ailbZ6jJV_Y& ved = 0ahUKEwjli8P4jNrkAhUd4nMBHdvUBCcQ4dUDCAs& uact = 5"rel =" nofollow noreferrer> cumsum 值等于x乘df['col'].eq('x').cumsum(),然后为每个组创建一个数据帧,方法是使用df.iloc[]将该组第二行和列的值作为该组的第一个值,并将它们保存在字典中: /p>

Considering your column name is col , you can first group the dataframe taking a cumsum on the col where the value equals x by df['col'].eq('x').cumsum() , then for each group create a dataframe by taking the values from the 2nd row of that group and the columns as the first value of that group using df.iloc[] and save them in a dictionary:

d={f'df{i}':pd.DataFrame(g.iloc[1:].values,columns=g.iloc[0].values) for i,g in df.groupby(df['col'].eq('x').cumsum())}

print(d['df1']) x 0 0 1 1 2 2 3 3 4 4 print(d['df2']) x 0 23 1 34 2 5 3 6

更多推荐

如何基于列行拆分数据框

本文发布于:2023-11-30 15:25:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!