Pandas: How do I split multiple lists in columns into multiple rows?

Cord Kaldemeyer

I have a pandas DataFrame that looks like the following:

     bus_uid   bus_type    type                      obj_uid  \
0     biomass: DEB31    biomass  output       Simple_139804698384200   
0     biomass: DEB31    biomass   other                        duals   
0     biomass: DEB31    biomass   other                       excess   

                                         datetime  \
0   DatetimeIndex(['2015-01-01 00:00:00',  '2015-01-01 01:00:00',  '2015-01-01 02:00:00', ...   
0   DatetimeIndex(['2015-01-01 00:00:00',  '2015-01-01 01:00:00',  '2015-01-01 02:00:00', ...   
0   DatetimeIndex(['2015-01-01 00:00:00',  '2015-01-01 01:00:00',  '2015-01-01 02:00:00', ...   

                                           values  
0   [1.0, 2.0, 3.0, ...  
0   [4.0, 5.0, 6.0, ...  
0   [7.0, 8.0, 9.0, ...

And want to convert it into the following format:

     bus_uid   bus_type    type                          obj_uid  datetime             values
0     biomass: DEB31    biomass  output   Simple_139804698384200  2015-01-01 00:00:00  1.0
0     biomass: DEB31    biomass  output   Simple_139804698384200  2015-01-01 01:00:00  2.0
0     biomass: DEB31    biomass  output   Simple_139804698384200  2015-01-01 02:00:00  3.0
0     biomass: DEB31    biomass   other                    duals  2015-01-01 00:00:00  4.0
0     biomass: DEB31    biomass   other                    duals  2015-01-01 01:00:00  5.0
0     biomass: DEB31    biomass   other                    duals  2015-01-01 02:00:00  6.0
0     biomass: DEB31    biomass   other                   excess  2015-01-01 00:00:00  7.0
0     biomass: DEB31    biomass   other                   excess  2015-01-01 01:00:00  8.0
0     biomass: DEB31    biomass   other                   excess  2015-01-01 02:00:00  9.0

The columns datetime and values have the same dimension.

I have already asked a similar question here but couldn't manage to apply the solution for my problem with two columns.

What's the best way to convert the DataFrame into the required format?

Stefan

You could iterate through the rows to extract the Index and Series info from the cells. I don't think that reshaping methods work well when you need to extract info at the same time:

Sample data:

rows = 3
df = pd.DataFrame(data={'bus_uid': list(repeat('biomass: DEB31', rows)), 'type': list(repeat('biomass', 3)), 'id': ['id1', 'id2', 'id3'], 'datetime': list(repeat(pd.DatetimeIndex(start=datetime(2016,1,1), periods=3, freq='D'), rows)), 'values': list(repeat([1,2,3], rows))})

          bus_uid                                           datetime   id  \
0  biomass: DEB31  DatetimeIndex(['2016-01-01', '2016-01-02', '20...  id1   
1  biomass: DEB31  DatetimeIndex(['2016-01-01', '2016-01-02', '20...  id2   
2  biomass: DEB31  DatetimeIndex(['2016-01-01', '2016-01-02', '20...  id3   

      type     values  
0  biomass  [1, 2, 3]  
1  biomass  [1, 2, 3]  
2  biomass  [1, 2, 3]  

Build new DataFrame as you iterate through the DataFrame rows:

new_df = pd.DataFrame()
for index, cols in df.iterrows():
    extract_df = pd.DataFrame.from_dict({'datetime': cols.ix['datetime'], 'values': cols.ix['values']})
    extract_df = pd.concat([extract_df, cols.drop(['datetime', 'values']).to_frame().T], axis=1).fillna(method='ffill').fillna(method='bfill')
    new_df = pd.concat([new_df, extract_df], ignore_index=True)

to get:

    datetime  values         bus_uid   id     type
0 2016-01-01       1  biomass: DEB31  id1  biomass
1 2016-01-02       2  biomass: DEB31  id1  biomass
2 2016-01-03       3  biomass: DEB31  id1  biomass
3 2016-01-01       1  biomass: DEB31  id2  biomass
4 2016-01-02       2  biomass: DEB31  id2  biomass
5 2016-01-03       3  biomass: DEB31  id2  biomass
6 2016-01-01       1  biomass: DEB31  id3  biomass
7 2016-01-02       2  biomass: DEB31  id3  biomass
8 2016-01-03       3  biomass: DEB31  id3  biomass

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do i split a single row of columns into multiple rows and columns?

From Dev

How do I split multiple columns into rows by delimiter in Power BI?

From Dev

how do i split multiple rows in different column in pandas

From Java

Pandas split column of lists into multiple columns

From Dev

How do I split results containing multiple columns into two rows each

From Dev

How do I split two columns in two separate tables into joined multiple rows in a view?

From Dev

How do I drop rows from a Pandas dataframe based on data in multiple columns?

From Dev

How to split a div into multiple rows and columns in css?

From Dev

How do I split one row into multiple rows with Excel?

From Dev

How do I split Angular UI Bootstrap accordions into multiple columns?

From Dev

How do I return multiple columns and rows in a SQL Count Query?

From Dev

How do I select rows with conditions on multiple columns in R

From Dev

How to split a pandas dataframe into multiple columns

From Dev

Split multiple excel columns into rows

From Dev

Split string into multiple rows and columns

From Dev

How do I fill NA values in multiple columns in pandas?

From Dev

How do I calculate a pandas column with multiple columns as arguments?

From Dev

How to use multiple separators in a pandas Series and split into multiple rows

From Dev

How do I manage multiple growing lists?

From Dev

How do I map multiple lists with dapper

From Dev

Split series containing lists of strings into multiple columns

From Dev

How do I split an audio file into multiple?

From Dev

pandas apply function to multiple columns and multiple rows

From Dev

How can I aggregate on multiple columns in pandas?

From Dev

How can I aggregate on multiple columns in pandas?

From Dev

How do I split a single row into multiple rows and Insert into a table in Oracle?

From Dev

How to style multiple lists in columns?

From Dev

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

From Dev

How to split values of a cell in multiple rows in pandas data frame?

Related Related

  1. 1

    How do i split a single row of columns into multiple rows and columns?

  2. 2

    How do I split multiple columns into rows by delimiter in Power BI?

  3. 3

    how do i split multiple rows in different column in pandas

  4. 4

    Pandas split column of lists into multiple columns

  5. 5

    How do I split results containing multiple columns into two rows each

  6. 6

    How do I split two columns in two separate tables into joined multiple rows in a view?

  7. 7

    How do I drop rows from a Pandas dataframe based on data in multiple columns?

  8. 8

    How to split a div into multiple rows and columns in css?

  9. 9

    How do I split one row into multiple rows with Excel?

  10. 10

    How do I split Angular UI Bootstrap accordions into multiple columns?

  11. 11

    How do I return multiple columns and rows in a SQL Count Query?

  12. 12

    How do I select rows with conditions on multiple columns in R

  13. 13

    How to split a pandas dataframe into multiple columns

  14. 14

    Split multiple excel columns into rows

  15. 15

    Split string into multiple rows and columns

  16. 16

    How do I fill NA values in multiple columns in pandas?

  17. 17

    How do I calculate a pandas column with multiple columns as arguments?

  18. 18

    How to use multiple separators in a pandas Series and split into multiple rows

  19. 19

    How do I manage multiple growing lists?

  20. 20

    How do I map multiple lists with dapper

  21. 21

    Split series containing lists of strings into multiple columns

  22. 22

    How do I split an audio file into multiple?

  23. 23

    pandas apply function to multiple columns and multiple rows

  24. 24

    How can I aggregate on multiple columns in pandas?

  25. 25

    How can I aggregate on multiple columns in pandas?

  26. 26

    How do I split a single row into multiple rows and Insert into a table in Oracle?

  27. 27

    How to style multiple lists in columns?

  28. 28

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

  29. 29

    How to split values of a cell in multiple rows in pandas data frame?

HotTag

Archive