3

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 :)

sariii
  • 1,725
  • 3
  • 20
  • 44
  • 3
    Don't you just want `df.sentences.tolist()`? You can then join that if you want. – user3483203 Aug 09 '18 at 22:03
  • you are right , It just gave me the correct answer. thank you so much. do you want add your answer? – sariii Aug 09 '18 at 22:06
  • No thanks, I think this might be a duplicate question, just happy to help :) – user3483203 Aug 09 '18 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 :) – sariii Aug 09 '18 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 – user3483203 Aug 09 '18 at 22:20

1 Answers1

2

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.

Steven
  • 704
  • 1
  • 6
  • 19