transform data from a Dataframe column into a single string

Asked

Viewed 88 times

1

I have a Dataframe with a column that has different texts in each row and I intend to join all the lines in a single string, it is possible?

the idea is to turn all the sentences in the column into one text and then perform analyses with nltk.

1 answer

1


You can use the to_string

df['Coluna'].to_string()

import pandas as pd

palavras = ['ola','como','vai','você?']
dados = pd.DataFrame({'Texto': palavras})
dados

Dice

    Texto
0   olá
1   como
2   vai
3   você?

Turning into a string

dados['Texto'].to_string(index=False).strip().replace('\n','')

Exit

'olá  como   vai você?'
  • but in that case I’ll change the column type, what I want to do is turn all the column rows into a single string.

  • @Victor, good morning! This command transforms the column lines into a single string. Hug!

  • 1

    good, thank you! when I tested this method the string was formed only with what was visible in the Dataframe, so if the text was large and the Dataframe view was '...', the string would take the phrase cut with the ellipsis. I used pd.set_option('max_colwidth', False) to solve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.