Python Forum
Slittping table into Multiple tables by rows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Slittping table into Multiple tables by rows (/thread-35167.html)



Slittping table into Multiple tables by rows - drunkenneo - Oct-06-2021

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.


RE: Slittping table into Multiple tables by rows - snippsat - Oct-06-2021

(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