我有一个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
更多推荐
如何基于列行拆分数据框
发布评论