2015-10-13 1 views
5

Ho recentemente iniziato a utilizzare il modulo nltk per l'analisi del testo. Sono bloccato in un punto. Voglio usare word_tokenize su un dataframe, in modo da ottenere tutte le parole usate in una particolare riga del dataframe.come utilizzare word_tokenize nel frame di dati

data example: 
     text 
1. This is a very good site. I will recommend it to others. 
2. Can you please give me a call at 9983938428. have issues with the listings. 
3. good work! keep it up 
4. not a very helpful site in finding home decor. 

expected output: 

1. 'This','is','a','very','good','site','.','I','will','recommend','it','to','others','.' 
2. 'Can','you','please','give','me','a','call','at','9983938428','.','have','issues','with','the','listings' 
3. 'good','work','!','keep','it','up' 
4. 'not','a','very','helpful','site','in','finding','home','decor' 

Fondamentalmente, voglio separare tutte le parole e trovare la lunghezza di ogni testo nel dataframe.

So che word_tokenize può essere utilizzato per una stringa, ma come applicarlo all'intero dataframe?

Si prega di aiuto!

Grazie in anticipo ...

+0

La descrizione del problema è priva di input di dati, codice, output desiderato. Grazie – EdChum

+0

@EdChum: hai modificato la query. Spero che abbia le informazioni richieste. – eclairs

risposta

9

È possibile utilizzare applicano metodo dataframe API:

import pandas as pd 
import nltk 

df = pd.DataFrame({'sentences': ['This is a very good site. I will recommend it to others.', 'Can you please give me a call at 9983938428. have issues with the listings.', 'good work! keep it up']}) 
df['tokenized_sents'] = df.apply(lambda row: nltk.word_tokenize(row['sentences']), axis=1) 

uscita:

>>> df 
              sentences \ 
0 This is a very good site. I will recommend it ... 
1 Can you please give me a call at 9983938428. h... 
2        good work! keep it up 

            tokenized_sents 
0 [This, is, a, very, good, site, ., I, will, re... 
1 [Can, you, please, give, me, a, call, at, 9983... 
2      [good, work, !, keep, it, up] 

Per trovare la lunghezza di ogni testo cercare di utilizzare applicare e funzione lambda di nuovo:

df['sents_length'] = df.apply(lambda row: len(row['tokenized_sents']), axis=1) 

>>> df 
              sentences \ 
0 This is a very good site. I will recommend it ... 
1 Can you please give me a call at 9983938428. h... 
2        good work! keep it up 

            tokenized_sents sents_length 
0 [This, is, a, very, good, site, ., I, will, re...   14 
1 [Can, you, please, give, me, a, call, at, 9983...   15 
2      [good, work, !, keep, it, up]    6 
+1

come possiamo farlo quando ci sono più righe nel dataframe? – eclairs

+0

@eclairs, cosa intendi? – Gregg

+0

Viene visualizzato questo messaggio di errore durante il tentativo di tokenize: – eclairs

10

pandas.Series.apply è più veloce di pandas.DataFrame.apply

import pandas as pd 
import nltk 

df = pd.read_csv("/path/to/file.csv") 

start = time.time() 
df["unigrams"] = df["verbatim"].apply(nltk.word_tokenize) 
print "series.apply", (time.time() - start) 

start = time.time() 
df["unigrams2"] = df.apply(lambda row: nltk.word_tokenize(row["verbatim"]), axis=1) 
print "dataframe.apply", (time.time() - start) 

Su un file CSV 125 MB campione,

series.apply 144,428858995

dataframe.apply 201.884778976

Modifica: Si potrebbe pensare il dataframe df dopo series.apply (nltk.word_tokenize) è più grande in termini di dimensioni, che potrebbero influire sul tempo di esecuzione per la prossima operazione dataframe.apply (nltk.word_tokenize) .

Panda ottimizza sotto il cofano per tale scenario. Ho ottenuto un runtime simile a 200s eseguendo esclusivamente dataframe.apply (nltk.word_tokenize) separatamente.