6

I have a dataframe like this

Have been on it 12 days along with 60 mg prozac 4+ years on that. index sentences

  1   I feel the best I have felt in years.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  2   "I have taken for over 7 years.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
  3   I slept 2 hours".                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  4   IT SAVED MY LIFE                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  5   IT SAVED MY LIFE" 

then I want to concatenate them in one array. the problem is that there maybe some sentences reptitive but still I want to keep all of them so the result will be:

["I feel the best I have felt in years", "I have taken for over 7 years." , "I slept 2 hours." , "IT SAVED MY LIFE" , "IT SAVED MY LIFE"]

I have tries this link and this which both are in r.

I have also tried this approach:

dfsent.groupby(['sentences']).apply(','.join)

but as some of the rows in my data frame are repetitive it give me only one of them. in case of my example return me this:

["I feel the best I have felt in years", "I have taken for over 7 years." , "I slept 2 hours." , "IT SAVED MY LIFE" ]

Thanks in advance :)

5
  • 3
    Don't you just want df.sentences.tolist()? You can then join that if you want. Commented Aug 9, 2018 at 22:03
  • you are right , It just gave me the correct answer. thank you so much. do you want add your answer? Commented Aug 9, 2018 at 22:06
  • No thanks, I think this might be a duplicate question, just happy to help :) Commented Aug 9, 2018 at 22:07
  • well its not duplicate at all, as in the question it want to convert to a list but my purpose was to concatenate them. so I did not know I can find this question with putting my word in another way, thanks by the way :) Commented Aug 9, 2018 at 22:11
  • Do you want a single string as the result? If so use df.sentences.str.cat(sep=',') I think that might be what you really want, if that's the case I'll reopen the question as not a dupe Commented Aug 9, 2018 at 22:20

1 Answer 1

9

If all you want is to produce a list of all the values (unique or not) in a column in your Pandas dataframe, the easier method would be to use the .tolist() method.

So, dfsent['sentences'].tolist() would produce the desired output.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.