Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of Series to Dataframe

I have a list having Pandas Series objects, which I've created by doing something like this:

li = []
li.append(input_df.iloc[0])
li.append(input_df.iloc[4])

where input_df is a Pandas Dataframe

I want to convert this list of Series objects back to Pandas Dataframe object, and was wondering if there is some easy way to do it

like image 671
Saurabh Verma Avatar asked Apr 02 '19 15:04

Saurabh Verma


People also ask

How do I convert a series to a Dataframe?

To begin, here is the syntax that you may use to convert your Series to a DataFrame: df = my_series.to_frame () Alternatively, you can use this approach to convert your Series: df = pd.DataFrame (my_series)

What is the difference between series and Dataframe?

Series can only contain single list with index, whereas dataframe can be made of more than one series or we can say that a dataframe is a collection of series that can be used to analyse the data. We are combining two series Author and Article published.

How to combine two series in a pandas Dataframe?

Create a dictionary so that we can combine the metadata for series. Metadata is the data of data that can define the series of values. Pass this dictionary to pandas DataFrame and finally you can see the result as combination of two series i.e for author and number of articles.

How to convert a list to a Dataframe?

They are adding values to a list and the data type for li is a list. So to convert the list to dataframe then they should use pd.Dataframe (<list name>). Since the right answer has got hidden in the comments, I thought it would be better to mention it as an answer: This did not work for me.


Video Answer


3 Answers

Based on the post you can do this by doing:

pd.DataFrame(li)

To everyone suggesting pd.concat, this is not a Series anymore. They are adding values to a list and the data type for li is a list. So to convert the list to dataframe then they should use pd.Dataframe(<list name>).

like image 142
Nicolaskn Avatar answered Sep 30 '22 23:09

Nicolaskn


Since the right answer has got hidden in the comments, I thought it would be better to mention it as an answer:

pd.concat(li, axis=1).T

will convert the list li of Series to DataFrame

like image 37
Saurabh Verma Avatar answered Sep 30 '22 22:09

Saurabh Verma


It seems that you wish to perform a customized melting of your dataframe. Using the pandas library, you can do it with one line of code. I am creating below the example to replicate your problem:

import pandas as pd
input_df = pd.DataFrame(data={'1': [1,2,3,4,5]
                          ,'2': [1,2,3,4,5]
                          ,'3': [1,2,3,4,5]
                          ,'4': [1,2,3,4,5]
                          ,'5': [1,2,3,4,5]})

Using pd.DataFrame, you will be able to create your new dataframe that melts your two selected lists:

li = []
li.append(input_df.iloc[0])
li.append(input_df.iloc[4])
new_df = pd.DataFrame(li)

if what you want is that those two lists present themselves under one column, I would not pass them as list to pass those list back to dataframe. Instead, you can just append those two columns disregarding the column names of each of those columns.

new_df = input_df.iloc[0].append(input_df.iloc[4])

Let me know if this answers your question.

like image 28
RenauV Avatar answered Sep 30 '22 22:09

RenauV