Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas Series to Dataframe using Series indexes as columns

Tags:

python

pandas

I have a Series, like this:

series = pd.Series({'a': 1, 'b': 2, 'c': 3}) 

I want to convert it to a dataframe like this:

    a   b   c 0   1   2   3 

pd.Series.to_frame() does't work, it got result like,

    0 a   1 b   2 c   3 

How can I construct a DataFrame from Series, with index of Series as columns?

like image 954
Jialin Zou Avatar asked Oct 24 '16 17:10

Jialin Zou


People also ask

How do you convert the index of a series into a column of a DataFrame?

To reset the index in pandas, you simply need to chain the function . reset_index() with the dataframe object. On applying the . reset_index() function, the index gets shifted to the dataframe as a separate column.

Can pandas series have index?

Pandas with PythonLabels can be called indexes and data present in a series called values. If you want to get labels and values individually. Then we can use the index and values attributes of the Series object. Let's take an example and see how these attributes will work.

How do you convert a series to a DataFrame in Python?

to_frame() function is used to convert the given series object to a dataframe. Parameter : name : The passed name should substitute for the series name (if it has one). Example #1: Use Series.


1 Answers

You can also try this :

df = DataFrame(series).transpose() 

Using the transpose() function you can interchange the indices and the columns. The output looks like this :

    a   b   c 0   1   2   3 
like image 109
PJay Avatar answered Sep 23 '22 03:09

PJay