Issue
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.
Solution
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')
Answered By - Marco_CH
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.