Convert pandas Series to DataFrame
Asked Answered
K

8

176

I have a Pandas series sf:

email
[email protected]    [1.0, 0.0, 0.0]
[email protected]    [2.0, 0.0, 0.0]
[email protected]    [1.0, 0.0, 0.0]
[email protected]    [4.0, 0.0, 0.0]
[email protected]    [1.0, 0.0, 3.0]
[email protected]    [1.0, 5.0, 0.0]

And I would like to transform it to the following DataFrame:

index | email             | list
_____________________________________________
0     | [email protected]  | [1.0, 0.0, 0.0]
1     | [email protected]  | [2.0, 0.0, 0.0]
2     | [email protected]  | [1.0, 0.0, 0.0]
3     | [email protected]  | [4.0, 0.0, 0.0]
4     | [email protected]  | [1.0, 0.0, 3.0]
5     | [email protected]  | [1.0, 5.0, 0.0]

I found a way to do it, but I doubt it's the more efficient one:

df1 = pd.DataFrame(data=sf.index, columns=['email'])
df2 = pd.DataFrame(data=sf.values, columns=['list'])
df = pd.merge(df1, df2, left_index=True, right_index=True)
Kalbli answered 29/9, 2014 at 10:38 Comment(1)
In more recent versions of pandas, this can be achieved with a single reset_index call.Wilfredowilfrid
W
230

Rather than create 2 temporary dfs you can just pass these as params within a dict using the DataFrame constructor:

pd.DataFrame({'email':sf.index, 'list':sf.values})

There are lots of ways to construct a df, see the docs

Wikiup answered 29/9, 2014 at 10:59 Comment(1)
another great option is to concat if your series have the same axes pd.concat([sf.index, sf.values], axis=1)Plumbago
P
105

to_frame():

Starting with the following Series, df:

email
[email protected]    A
[email protected]    B
[email protected]    C
dtype: int64

I use to_frame to convert the series to DataFrame:

df = df.to_frame().reset_index()

    email               0
0   [email protected]    A
1   [email protected]    B
2   [email protected]    C
3   [email protected]    D

Now all you need is to rename the column name and name the index column:

df = df.rename(columns= {0: 'list'})
df.index.name = 'index'

Your DataFrame is ready for further analysis.

Update: I just came across this link where the answers are surprisingly similar to mine here.

Pacifa answered 3/12, 2016 at 0:58 Comment(2)
series_obj.to_frame() works! I output this class type <class 'pandas.core.frame.DataFrame'>Mathes
Why use to_frame().reset_index() rather than just reset_index? You could even just do reset_index(name='list')Iolite
C
36

One line answer would be

myseries.to_frame(name='my_column_name')

Or

myseries.reset_index(drop=True, inplace=True)  # As needed
Chong answered 28/8, 2018 at 10:43 Comment(1)
I need to use the converted DataFrame obj in seaborn bar plot, so I need to reset index, say, mydf = myseries.to_frame(name='my_column_name').reset_index()Interested
W
28

Series.reset_index with name argument

Often the use case comes up where a Series needs to be promoted to a DataFrame. But if the Series has no name, then reset_index will result in something like,

s = pd.Series([1, 2, 3], index=['a', 'b', 'c']).rename_axis('A')
s

A
a    1
b    2
c    3
dtype: int64

s.reset_index()

   A  0
0  a  1
1  b  2
2  c  3

Where you see the column name is "0". We can fix this be specifying a name parameter.

s.reset_index(name='B')

   A  B
0  a  1
1  b  2
2  c  3

s.reset_index(name='list')

   A  list
0  a     1
1  b     2
2  c     3

Series.to_frame

If you want to create a DataFrame without promoting the index to a column, use Series.to_frame, as suggested in this answer. This also supports a name parameter.

s.to_frame(name='B')

   B
A   
a  1
b  2
c  3

pd.DataFrame Constructor

You can also do the same thing as Series.to_frame by specifying a columns param:

pd.DataFrame(s, columns=['B'])

   B
A   
a  1
b  2
c  3
Wilfredowilfrid answered 8/12, 2018 at 19:46 Comment(4)
I was wondering why one might use to_frame instead of reset_index, but is there ever a good reason to use both? hereIolite
@Iolite mostly utility. If you want a single col dataframe with index, use to_frame(). If you need two columns (one from the series index and the other from series values itself), go with reset_index().Wilfredowilfrid
And what if I want to convert Series to DataFrame with Seires index used as DataFrame columns names (i.e. transposed)? to_frame doesn't seem to have an argument to do this. Thanks.Extravasate
@Extravasate use to_frame().T to transpose itWilfredowilfrid
P
12

Super simple way is also

df = pd.DataFrame(series)

It will return a DF of 1 column (series values) + 1 index (0....n)

Priggish answered 13/1, 2021 at 16:19 Comment(1)
So, pd.DataFrame(series, columns=['list']).rename_axis('email') might be good.Jana
S
5

Series.to_frame can be used to convert a Series to DataFrame.

# The provided name (columnName) will substitute the series name
df = series.to_frame('columnName')

For example,

s = pd.Series(["a", "b", "c"], name="vals")
df = s.to_frame('newCol')
print(df)

   newCol
0    a
1    b
2    c
Sawfish answered 19/2, 2020 at 11:54 Comment(0)
F
2

probably graded as a non-pythonic way to do this but this'll give the result you want in a line:

new_df = pd.DataFrame(zip(email,list))

Result:

               email               list
0   [email protected]    [1.0, 0.0, 0.0]
1   [email protected]    [2.0, 0.0, 0.0]
2   [email protected]    [1.0, 0.0, 0.0]
3   [email protected]    [4.0, 0.0, 3.0]
4   [email protected]    [1.0, 5.0, 0.0]
Fokos answered 30/7, 2020 at 14:11 Comment(0)
O
0

The series.to_frame() method and pd.DataFrame() method is used to convert Pandas Series to a DataFrame.

import pandas
series = pandas.Series([40, 90, 80, 50, 70])
data_frame = series.to_frame()
print(data_frame)

or use the below:

data_frame = pandas.DataFrame(series)

The pd.concat() method is used to convert multiple Series to a single DataFrame in Python.

data_frame = pandas.concat([series1, series2], axis=1)
Osculation answered 5/8, 2023 at 6:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.