Split a Pandas Dataframe into multiple smaller dataframes based on empty rows

GreenGodot

I have a csv file with a format like this:

Header 1, Header 2, Header 3
''          ''        ''
value 1,  value2,   value 3
value 1,  value2,   value 3
value 1,  value2,   value 3
''          ''        ''
value 1,  value 2,   value 3
value 1,  value 2,   value 3
value 1,  value 2,   value 3
 ''          ''        ''

I can read it into a pandas dataframe but the segments surrounded by empty rows (denoted by '') need to be each processed individually. What would be the simplest way to divide them into smaller dataframes based off of them being between empty rows? I have quite a few of these segments to go through.

Would it be easier to divide them into smaller dataframes or would removing the segment from the original dataframe after processing it be even easier?

EDIT:

IanS's answer was correct but in my case some of my files had simply no quotes in empty rows so the type was not a string. I modified his answer a little and this worked for them:

df['counter'] = (df['Header 1'].isnull()).cumsum()
df = df[df['Header 1'].isnull() == False]  # remove empty rows
df.groupby('counter').apply(lambda df: df.iloc[0])
IanS

The simplest would be to add a counter that increments each time it encounters an empty row. You can then get your individual dataframes via groupby.

df['counter'] = (df['Header1'] == "''").cumsum()
df = df[df['Header1'] != "''"]  # remove empty rows
df.groupby('counter').apply(lambda df: df.iloc[0])

The last line applies your processing function to each dataframe separately (I just put a dummy example).

Note that the exact condition testing for empty rows (here df['Header1'] == "''") should be adapted to your exact situation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas - Split dataframe into multiple dataframes based on dates?

From Dev

Split pandas dataframe into multiple dataframes with equal numbers of rows

From Java

Split pandas dataframe into multiple dataframes based on null columns

From Dev

Split pandas dataframe into multiple dataframes based on null columns

From Dev

Split a Pandas Dataframe into multiple Dataframes based on Triangular Number Series

From Dev

Pandas: Split data frame based on empty rows

From Dev

Split one dataframe to multiple sub-dataframes based on common columns in Pandas

From Dev

Split cell into multiple rows in pandas dataframe

From Dev

PANDAS split dataframe to multiple by unique values rows

From Dev

Split Python Dataframe into multiple Dataframes (where chosen rows are the same)

From Dev

Split a pandas dataframe into two dataframes efficiently based on some condition

From Dev

Modify pandas dataframe in python based on multiple rows

From Dev

Pandas Split Dataframe into two Dataframes

From Dev

Separating a dataframe into multiple dataframes based on the index value in pandas

From Dev

pandas: fill multiple empty dataframes

From Dev

pandas: fill multiple empty dataframes

From Dev

Split a column containing a list into multiple rows in Pandas based on a condition

From Dev

How to loop through Pandas DataFrame and split a string into multiple rows

From Dev

Merge Multiple Duplicate rows based on multiple columns in Pandas.Dataframe

From Dev

Selecting rows from a Dataframe based on values in multiple columns in pandas

From Java

Drop rows on multiple conditions (based on 2 column) in pandas dataframe

From Dev

selecting rows based on multiple column values in pandas dataframe

From Dev

Deleting DataFrame rows in Pandas based on column value - multiple values to remove

From Dev

Select rows from a DataFrame based on multiple values in a column in pandas

From Dev

Performing calculation based off multiple rows in Pandas dataframe

From Dev

Pandas DataFrame, how to calculate a new column element based on multiple rows

From Java

How to split dataframe into multiple dataframes by column index

From Dev

Split rows into multiple rows based on quantity

From Dev

Split pandas dataframe based on groupby

Related Related

  1. 1

    Pandas - Split dataframe into multiple dataframes based on dates?

  2. 2

    Split pandas dataframe into multiple dataframes with equal numbers of rows

  3. 3

    Split pandas dataframe into multiple dataframes based on null columns

  4. 4

    Split pandas dataframe into multiple dataframes based on null columns

  5. 5

    Split a Pandas Dataframe into multiple Dataframes based on Triangular Number Series

  6. 6

    Pandas: Split data frame based on empty rows

  7. 7

    Split one dataframe to multiple sub-dataframes based on common columns in Pandas

  8. 8

    Split cell into multiple rows in pandas dataframe

  9. 9

    PANDAS split dataframe to multiple by unique values rows

  10. 10

    Split Python Dataframe into multiple Dataframes (where chosen rows are the same)

  11. 11

    Split a pandas dataframe into two dataframes efficiently based on some condition

  12. 12

    Modify pandas dataframe in python based on multiple rows

  13. 13

    Pandas Split Dataframe into two Dataframes

  14. 14

    Separating a dataframe into multiple dataframes based on the index value in pandas

  15. 15

    pandas: fill multiple empty dataframes

  16. 16

    pandas: fill multiple empty dataframes

  17. 17

    Split a column containing a list into multiple rows in Pandas based on a condition

  18. 18

    How to loop through Pandas DataFrame and split a string into multiple rows

  19. 19

    Merge Multiple Duplicate rows based on multiple columns in Pandas.Dataframe

  20. 20

    Selecting rows from a Dataframe based on values in multiple columns in pandas

  21. 21

    Drop rows on multiple conditions (based on 2 column) in pandas dataframe

  22. 22

    selecting rows based on multiple column values in pandas dataframe

  23. 23

    Deleting DataFrame rows in Pandas based on column value - multiple values to remove

  24. 24

    Select rows from a DataFrame based on multiple values in a column in pandas

  25. 25

    Performing calculation based off multiple rows in Pandas dataframe

  26. 26

    Pandas DataFrame, how to calculate a new column element based on multiple rows

  27. 27

    How to split dataframe into multiple dataframes by column index

  28. 28

    Split rows into multiple rows based on quantity

  29. 29

    Split pandas dataframe based on groupby

HotTag

Archive