0

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

1
  • 1
    Do you want to have 6 columns, 'Name' and the letters a-e or just have a-e? Commented Jul 7, 2022 at 3:25

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

@AK007 Sure, I've updated the code renaming the column names

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.