2

I have a dataframe on this format:

    id  val
0   294 aa
1   254 bb
2   844 cc

I need the 'val' column to be a list with the string inside, since I need to join the dataframe with another dataframe with this format:

id  val
0   294 [aa]
1   254 [bb]
2   844 [cc]

Anyone know how I can accomplish this? It has to be a list, seeing as the other dataframe is the format of our DB, which I want to insert the joined dataframe into.

2 Answers 2

1

I advise against storing non-scalar types but if you insist you can use apply and construct a list for each row:

In [53]:
df['val'] = df['val'].apply(lambda x: [x])
df

Out[53]:
    id   val
0  294  [aa]
1  254  [bb]
2  844  [cc]
Sign up to request clarification or add additional context in comments.

Comments

0

You can use:

df['val'] = [[x] for x in df['val'].values.tolist()]
print (df)
    id   val
0  294  [aa]
1  254  [bb]
2  844  [cc]

But better is using scalar, for remove list is best str[0]:

print (df)
    id   val
0  294  [aa]
1  254  [bb]
2  844  [cc]

df['val'] = df['val'].str[0]
print (df)
    id val
0  294  aa
1  254  bb
2  844  cc

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.