Python Forum

Full Version: Slittping table into Multiple tables by rows
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a dataframe which have data in it eg:

A B C
1 2 3
4 2 3
D F C
4 5 7

I want the dataframe to split into two different dataframes as below

A B C
1 2 3
4 2 3

and

D F C
4 5 7




Is there any way to do it in python, kindly help.
(Oct-06-2021, 11:55 AM)drunkenneo Wrote: [ -> ]Is there any way to do it in python, kindly help.
Yes,next time post some working code and what you tried.
Then it show that you put some effort into it,and it easier to get help.
import pandas as pd
from io import StringIO

data = StringIO("""\
A B C
1 2 3
4 2 3
D F C
4 5 7""")

df = pd.read_csv(data, sep=" ")
>>> df
   A  B  C
0  1  2  3
1  4  2  3
2  D  F  C
3  4  5  7
>>> 
>>> df_1 = df.iloc[:2,:]
>>> df_2 = df.iloc[2:,:]
>>> 
>>> df_1
   A  B  C
0  1  2  3
1  4  2  3
>>> 
>>> df_2.columns = df_2.iloc[0] 
>>> df_2 = df_2[1:]
>>> df_2
2  D  F  C
3  4  5  7