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 split data inside list into individual separate columns using pandas

I want to split data inside Names into individual separate columns using pandas

import pandas as pd

name_dict = {
            'Name': ['a|b|c|d|e']
          }

df = pd.DataFrame(name_dict)

print (df)

enter image description here

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 can use str.split() to split value by |, as follows:

import pandas as pd

name_dict = {
    'Name': ['a|b|c|d|e']
}

df = pd.DataFrame(name_dict)
print(df)
#        Name
#0  a|b|c|d|e

df = df['Name'].str.split('|', expand=True)
print(df)
#   0  1  2  3  4
#0  a  b  c  d  e

# to change column names
df = df.rename(
    columns={
        0: 'a',
        1: 'b',
        2: 'c',
        3: 'd',
        4: 'e',
    }
)
print(df)
#   a  b  c  d  e
#0  a  b  c  d  e

For more information, such as advanced options, please refer to https://pandas.pydata.org/docs/reference/api/pandas.Series.str.split.html

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