0

I have a dataframe with two series x and y. I want to merge them to create a new series: tag, but I'm not able to achieve the expected output. I've tried:

df['tag'] = df['x'] + df['y']

I've looked everywhere and haven't been able to find a solution to the problem.

Current output:

x                               y           tag
['fast food', 'american']   ['chicken'] ['fast food', 'american']['chicken']

Expected output:

x                               y           tag
['fast food', 'american']   ['chicken'] ['fast food', 'american', 'chicken']

df.to_dict()

{'x': "['fast food', 'american']",
 'y': "['chicken']"}

4 Answers 4

2

I do not think that is list , so you may convert it into list , them you can sum

import ast
df.x = df.x.apply(ast.literal_eval)
df.y = df.y.apply(ast.literal_eval)

df['tag'] = df['x'] + df['y']

More info

df=pd.DataFrame()
df['y']=["['chicken']"]
df['x']=["['fast food', 'american']"]
df.applymap(type)
Out[295]: 
               y              x
0  <class 'str'>  <class 'str'>


df.x = df.x.apply(ast.literal_eval)
df.y = df.y.apply(ast.literal_eval)
df.applymap(type)
Out[297]: 
                y               x
0  <class 'list'>  <class 'list'>
Sign up to request clarification or add additional context in comments.

3 Comments

With this solution I must have a string list inside a list, how can I do that?
@Pepe in the mean time . convert it by df=df.astype(str), then using above method
@Pepe did you try my method , ? df.x = df.x.apply(ast.literal_eval) df.y = df.y.apply(ast.literal_eval) df['tag'] = df['x'] + df['y'], this solved the problem
0

Another way is to use re.findall:

import re

df.applymap(lambda x:re.findall("'(.+?)'", x)).sum(1)

which will return a list of strs:

                           x            y                             tag
0  ['fast food', 'american']  ['chicken']  [fast food, american, chicken]

Comments

0

You can unpack the list and use apply to create your new column. enter image description here

Comments

-1

UPDATED

Try this :

df=pd.DataFrame()
df['X']=[["chicken"]]
df['Y']=[["fast food","American"]]
df['tag']=df['X']+df['Y']

2 Comments

returns the same result I posted.
Apologies @Pepe . Updated the answer. Check it out

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.