Estoy usando el siguiente código para calificar el texto
import textstat
import pandas as pd
test_data = ("""Jonathan pushed back the big iron pot and stood up.
There were no bears. But up the path came his father, carrying his gun. And with
him were Jonathan's Uncle James and his Uncle Samuel, his Uncle John and his
Uncle Peter. Jonathan had never in all his life been so glad to see the uncles.
"Jonathan!'" said his father, "what a fright you have given us! Where have you
been all this time?" """)
textstat.flesch_reading_ease(test_data)
Da un puntaje de 100.48 (muy fácil de leer)
Tengo un csv con columnas 'Título' y 'Texto'. Quiero recorrer cada fila y usar la función textstat.flesch_reading_ease en cada celda de la columna 'Texto'.
Sin embargo, parece que no puedo entenderlo bien.
import textstat
import pandas as pd
csv = pd.read_csv('my_list_of_texts.csv')
for i, j in csv.iterrows():
a = textstat.flesch_reading_ease(j)
print(a)
Esto me da el error TypeError: los objetos 'Series' son mutables, por lo tanto no pueden ser hash
3 respuestas
Resolví esto colocando textstat.flesch_reading_ease en su propia función y eliminando NaNs de la serie.
def readability_score(text):
s = textstat.automated_readability_index(text)
return s
csv = csv.dropna()
Apply está aquí para hacer el trabajo en pandas DataFrame y Series.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html
Y puedes encontrar algunos ejemplos aquí:
https://chrisalbon.com/python/data_wrangling/pandas_apply_operations_to_dataframes/
Utilice Series.apply:
csv['Text'].apply(textstat.flesch_reading_ease)
Preguntas relacionadas
Nuevas preguntas
python
Python es un lenguaje de programación multipropósito, de tipificación dinámica y de múltiples paradigmas. Está diseñado para ser rápido de aprender, comprender y usar, y hacer cumplir una sintaxis limpia y uniforme. Tenga en cuenta que Python 2 está oficialmente fuera de soporte a partir del 01-01-2020. Aún así, para preguntas de Python específicas de la versión, agregue la etiqueta [python-2.7] o [python-3.x]. Cuando utilice una variante de Python (por ejemplo, Jython, PyPy) o una biblioteca (por ejemplo, Pandas y NumPy), inclúyala en las etiquetas.