Mahesh
Mahesh

Reputation: 1333

Splitting pandas dataframe in python on empty rows

I have a dataframe that has multiple tables with 1 or 2 empty rows in between. I want to split based on empty rows.

There are 3 tables here. As you can see, row no. 4,5, 13,14 are blank on which the split must happen.

                            Name              Data         
0                   Ramesh Raman                 1         
1                Shabbir Hussein                 2         
2                           Mark                 3         
3                           Jeff                 4         
4                                                          
5                                                          
6                        Company           Contact  Country
7            Alfreds Futterkiste      Maria Anders  Germany
8     Centro comercial Moctezuma   Francisco Chang   Mexico
9                   Ernst Handel     Roland Mendel  Austria
10                Island Trading     Helen Bennett       UK
11  Laughing Bacchus Winecellars   Yoshi Tannamuri   Canada
12  Magazzini Alimentari Riuniti  Giovanni Rovelli    Italy
13                                                         
14                                                         
15                          Name            Weight   Symbol
16                      Hydrogen            1.0079        H
17                        Helium            4.0026       He
18                       Lithium             6.941       Li
19                     Beryllium            9.0122       Be
20                         Boron            10.811        B

I have tried the following answer from another SFO Post, but no luck.

df_list = np.split(df, df[df.isnull().all(1)].index) 

for df in df_list:
    print(df, '\n')

Please suggest a workaround. Thanks.

Upvotes: 1

Views: 702

Answers (1)

Marco_CH
Marco_CH

Reputation: 3294

I think in your example the rows are not NaN, it seems they're having an empty string.

Plese try the following code:

df_list = np.split(df, df[df.eq("").all(1)].index) 

for df in df_list:
    print(df, '\n')

Upvotes: 1

Related Questions