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 create a list from Pandas Series?

My dataframe looks like this. I am trying to create a list of Names. For ex: ["Mike", "Jean"]:

0. Mike, Jean
1. May, Weather
2. Jack, 100

What I’ve tried:

df["NAME"] = df["NAME"].str.split(",")
for i in range(len(df["NAME"])):
    df["NAME"][i] = df["NAME"][i] .split(",")

OUTPUT

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

0. [Mike, Jean]
1. [May, Weather]
2. [Jack, 100]

OUTPUT I WANT

0. ["Mike", "Jean"]
1. ["May", "Weather"]
2. ["Jack", "100"]

I am new to Python and Pandas. Any help will be appreciated!

>Solution :

Assuming this input:

df = pd.DataFrame({'Name': ['Mike, Jean', 'May, Weather', 'Jack, 100']})

When you run:

df['Name'].str.split(', ')

and get:

0      [Mike, Jean]
1    [May, Weather]
2       [Jack, 100]
Name: Name, dtype: object

The [Mike, Jean] format is just a representation.

The real data is indeed a Series of lists, as show by an explicit conversion of the Series to list:

df['Name'].str.split(', ').to_list()

output:

[['Mike', 'Jean'],
 ['May', 'Weather'],
 ['Jack', '100']]
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