Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Data Science by (17.6k points)

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)

1 Answer

0 votes
by (41.4k points)
edited by

Instead of creating two temporary dfs, just pass these as params within a dict using the DataFrame constructor:

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

If you want to learn  Python for Data Science then you can watch this Python Tutorial:

Related questions

Browse Categories

...