1

I have a csv loaded into a panda dataframe. One of the columns contains semi colon delimited list of words like

Beach holiday;Plenty of space;Pool

and I would like to turn this into an array or collection like ["Beach holiday","Plenty of space","Pool"]

Alternatively could create a new column derived from the orginal.

Thank you!

1
  • 1
    Please provide example dataframe. Commented Nov 5, 2018 at 15:11

3 Answers 3

1

The recommended solution, especially if you have the same number of ; separators in each string, is to create a dataframe of object dtype series, with each element a single string:

df = pd.DataFrame({'A': ['Beach holiday;Plenty of space;Pool',
                         'Mountain holiday;Plenty of grey;Ice']})

df = df['A'].str.split(';', expand=True)

print(df)

                  0                1     2
0     Beach holiday  Plenty of space  Pool
1  Mountain holiday   Plenty of grey   Ice

Creating a series of lists, the alternative, is not recommended; it involves a nested layer of pointers.

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

Comments

0

You can do this if you want the headers to be in a list

list(df.columns.values)

or

df[col_name].tolist()

also, check this answer here

Comments

0

You may check converters

TESTDATA = StringIO("""
 A,B
1,Beach holiday;Plenty of space;Pool
1,Beach holiday;Plenty of space;Pool
    """)
df = pd.read_csv(TESTDATA ,converters={'B':lambda x : x.split(';')})
df
Out[147]: 
    A                                       B
0   1  [Beach holiday, Plenty of space, Pool]
1   1  [Beach holiday, Plenty of space, Pool]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.