Python function similar to unnest_tokens() from titytext in R

Viewed 32

I'm working on converting an R script I use for sentiment analysis into python, and while I have the rest of it worked out, I can't find anything in python similar to the unnest_tokens() function from the titytext library in R.

I have the message column of the DF tokenized using the word_tokenize function from the nltk library, but I can't figure out how to unpack those tokens so that each word occupies a single row in a new DF, while also maintaining the same Case Number, Created Date and Index number from the original DF.

If I haven't explained this clearly, please let me know. I appreciate any help.

  • Example of the R function I'm trying to replicate in Python - No help needed on this.
  • inbound_tidy is the name of the new DF and Response is the one I'm starting from.
inbound_tidy <- Response %>%
  unnest_tokens(word, Message)

The tables look okay in the preview, but they're badly formatted once publishing. I'm not sure why, but here's a picture of the tables from the preview as well.

tables

Name: df Data Frame I'm starting with imported from spreadsheet |Message|Case Number|Created Date|Index| |-------|-----------|------------|-----| |Thank..|65418 |4/26/2022 |1 | |You su.|38186 |6/45/2020 |2 |

Name: inbound Required output |Message|Case Number|Created Date|Index| |-------|-----------|------------|-----| |Thank |65418 |4/26/2022 |1 | |You |65418 |4/26/2022 |1 | |for |65418 |4/26/2022 |1 | |the |65418 |4/26/2022 |1 | |help |65418 |4/26/2022 |1 | |You |38186 |6/45/2020 |2 | |suck |38186 |6/45/2020 |2 |

0 Answers
Related