Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to turn column with lists into dataframe?

I have a dataset:

val
[0.5, 0.7,....,0.8]
[0.11, 0.4,....,0.77]
...
[0.99, 0.1,....,0.4]

it is pandas.core.series.Series

there are 1000 rows and 200 values in each list. I want to turn it into dataframe with 200 columns and 1000 rows, but when I do pd.DataFrame(list(df)) it says NumPy.float64 object is not iterable. How to fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use a dictionary comprehension:

import pandas as pd
s = pd.Series([[1,2,3],[4,5,6]])  # please provide a reproducible example
pd.DataFrame.from_dict({i:col for i,col in s.iteritems()})

returns

   0  1
0  1  4
1  2  5
2  3  6

You could also use from_records with a transpose:

pd.DataFrame.from_records(s).transpose()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading