1

I have a dataframe, for example:

a b c
0 1 2
3 4 5
6 7 8

and i need to separate it by rows and create a new dataframe from each row. i tried to iterate over the rows and then for each row (which is a seriese) i tried the command row.to_df() but it gives me a weird result.

basicly im looking to create bew dataframe sa such:

a b c
0 1 2


a b c
3 4 5


a b c
7 8 9
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
Gony
  • 25
  • 3
  • You are looking for this: https://stackoverflow.com/questions/19790790/splitting-dataframe-into-multiple-dataframes? – Roxy Aug 31 '21 at 16:25

2 Answers2

1

You can simply iterate row-by-row and use .to_frame(). For example:

for _, row in df.iterrows():
    print(row.to_frame().T)
    print()

Prints:

   a  b  c
0  0  1  2

   a  b  c
1  3  4  5

   a  b  c
2  6  7  8

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

You can try doing:

for _, row in df.iterrows():
   new_df = pd.DataFrame(row).T.reset_index(drop=True)

This will create a new DataFrame object from each row (Series Object) in the original DataFrame df.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ofek Glick
  • 999
  • 2
  • 9
  • 20