0

I have the following dataset:

df = pd.DataFrame([['Tree', 1], ['Tree, Hug']], columns=('Tag', 'ticketID'))

What I would like to do now I transform the tag category to a numpy array like this

df = df[['Tag']]
tags = df.values

This however gives me

[['Tree']
 ['Tree, Hug']]

While I am looking for

[['Tree']
 ['Tree', 'Hug']]

Any thoughts on how I can get this working?

0

2 Answers 2

1
>>> import pandas as pd
>>> df = pd.DataFrame([['Tree', 1], ['Tree, Hug']], columns=('Tag', 'ticketID'))
>>> [ x.split(', ') for row in df[['Tag']].values for x in row ]
[['Tree'], ['Tree', 'Hug']]

Be careful with the split call. Have to split ', ' here since original string has a space.

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

Comments

0

To keep everything in Pandas you can iterate over the Tag column with apply and convert with tolist

df.Tag.apply(lambda x: [s.strip() for s in x.split(',')]).tolist()

[['Tree'], ['Tree', 'Hug']]

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.