1

Please let me know how to convert list to str.

My dataframe(df_meta_review1) structure sample:

-----------------------------------------------------
userID    categories                    category_domain(I want a this!)
-----------------------------------------------------
a1010     [pc, game]                    pc,game-A
a1011     [mobile, game, education]     mobile, game, education-A

I already tried belows

1. df_meta_review1['category_domain'] = df_meta_review1['categories'].astype('str')    #error!

2. df_meta_review1['category_domain'] = df_meta_review1(['categories']).apply(','.join)  #error!

3. df_meta_review1['category_domain'] = ",".join(map(str, df_meta_review1(['categories'])))   #error!
1
  • 1
    What error are you getting? Commented Jul 16, 2019 at 6:24

2 Answers 2

1

I've recreated your table with the following code

df_meta_review1 = pd.DataFrame([('a101',['pc', 'game']),('a1001', ['mobile', 'game', 'education'])], columns = ['userID', 'categories'])

Running the first piece of code gives no error. Are you sure you get an error when its run? Below is the resultant table.

  userID                 categories                  category_domain
0   a101                 [pc, game]                   ['pc', 'game']
1  a1001  [mobile, game, education]  ['mobile', 'game', 'education']

If I run the second line, I also get an error. Like so:

TypeError: 'DataFrame' object is not callable

To fix this I remove the round parenthesis from after the data frame name, as the error suggests you we are trying to use the data frame as a function (this is what callable means)

df_meta_review1['category_domain'] = df_meta_review1['categories'].apply(','.join)

This no longer generates an error and the output looks like:

  userID                 categories        category_domain
0   a101                 [pc, game]                pc,game
1  a1001  [mobile, game, education]  mobile,game,education

Which, I think is what you are looking for. For the third line of code I get the error:

TypeError: 'DataFrame' object is not callable 

Again, dropping the round parenthesis from after the data frame name, yields code that executes without error.

df_meta_review1['category_domain'] = ",".join(map(str, df_meta_review1['categories']))

However I don't think it is what you are looking for.

  userID                 categories                                 category_domain
0   a101                 [pc, game]  ['pc', 'game'],['mobile', 'game', 'education']
1  a1001  [mobile, game, education]  ['pc', 'game'],['mobile', 'game', 'education']
Sign up to request clarification or add additional context in comments.

Comments

1

You can make use of apply function that loop through the dataframe and define a function to return the results for each row, like this:

def app(row):
    return ",".join(row['categories'])

and pass this to apply function:

df_meta_review1['category_domain'] = df_meta_review1.apply(lambda x: app(x),axis =1)

output:

  userID                 categories          category_domain
0   a101                 [pc, game]                  pc,game
1  a1001  [mobile, game, education]    mobile,game,education

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.