Convert pandas Series to DataFrame

PythonPandasDataframeSeries

Python Problem Overview


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)

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

to_frame():

Starting with the following Series, df:

email
email1@email.com    A
email2@email.com    B
email3@email.com    C
dtype: int64

I use to_frame to convert the series to DataFrame:

df = df.to_frame().reset_index()

    email	            0
0	email1@email.com	A
1	email2@email.com	B
2	email3@email.com	C
3	email4@email.com	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.

Solution 3 - Python

One line answer would be

myseries.to_frame(name='my_column_name')

Or

myseries.reset_index(drop=True, inplace=True)  # As needed

Solution 4 - Python

###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

Solution 5 - Python

Super simple way is also

df = pd.DataFrame(series)

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

Solution 6 - Python

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

Solution 7 - Python

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]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwoshitomView Question on Stackoverflow
Solution 1 - PythonEdChumView Answer on Stackoverflow
Solution 2 - PythonShoreshView Answer on Stackoverflow
Solution 3 - PythonMysteriousView Answer on Stackoverflow
Solution 4 - Pythoncs95View Answer on Stackoverflow
Solution 5 - PythonLorenzo BassettiView Answer on Stackoverflow
Solution 6 - PythonGiorgos MyrianthousView Answer on Stackoverflow
Solution 7 - PythonPrathamesh MistryView Answer on Stackoverflow