Home > Software design >  Separate multiple rows of data into data frames of one row each
Separate multiple rows of data into data frames of one row each

Time:01-26

There is a data frame that holds the data retrieved from the SQL DB. Assume that you have five rows of data as follows (I don't know how many rows there are in the actual data).

dfDB
   C1   C2   C3
1  A   112  7449
2  B   113  5711
3  C   113  8290
4  D   114  2784
5  E   113  4886

I want to create a data frame with these data separated by one rows.

dfDB_1
   C1   C2   C3
1  A   112  7449

dfDB_2
   C1   C2   C3
2  B   113  5711

dfDB_3
   C1   C2   C3
3  C   113  8290

dfDB_4
   C1   C2   C3
4  D   114  2784

dfDB_5
   C1   C2   C3
5  E   113  4886

Also, if possible, I would like to output the data of each data frame as an Excel file, is that possible? I am thinking of using SPO or Blob Storage to create Excel files.

dfDB_1 -> dfDB_1.xlsx
dfDB_2 -> dfDB_2.xlsx
dfDB_3 -> dfDB_3.xlsx
dfDB_4 -> dfDB_4.xlsx
dfDB_5 -> dfDB_5.xlsx

If anyone has any good ideas, could you please let me know?

Best regards, Lopez

CodePudding user response:

Create a new list df_row = [] and

df.apply(lambda x: df_row.append(df.loc[[x.name]]), axis=1)

Then for instance

df_row[0]
# Out: 
#   C1   C2    C3
# 0  A  112  7449

df_row[1]
# Out: 
#   C1   C2    C3
# 1  B  113  5711

type(df_row[0])
# Out: 
# pandas.core.frame.DataFrame

CodePudding user response:

Use iterrows() and list comprehension

list_of_dfs = row.to_frame().T for _,row in dfDB.iterrows()]

output (a list of dataframes)

[  C1   C2    C3
 1  A  112  7449,
   C1   C2    C3
 2  B  113  5711,
   C1   C2    C3
 3  C  113  8290,
   C1   C2    C3
 4  D  114  2784,
   C1   C2    C3
 5  E  113  4886]

eg

list_of_dfs[0]

output


    C1  C2  C3
1   A   112 7449

to save to excel use to_excel in a loop, eg:

for n,df in enumerate(list_of_dfs):
    df.to_excel(f'dfDB_{n}.xlsx')
  •  Tags:  
  • Related