textstat可以对文本进行可读性计算,支持英文、德语、西班牙、意大利、荷兰语等。目前不支持中文,如做中文文本分析,可以考虑用cntext包。之前分享过两篇可读性的内容
https://github.com/textstat/textstat
今天尝试将pandas和textstat结合起来,对csv做文本分析
今天在本文中,将学习三个知识点。
pip3 install textstat
import pandas as pd
import textstat
#设置dataframe显示的宽度
pd.options.display.max_colwidth = 100
df = pd.read_csv('data.csv')
df
Run
使用apply方法对pd.Series类型的数据进行批操作
extstat库有丰富的可读性方法,这里任选2个作为 批操作函数。
df['Fog'] = df['doc'].apply(textstat.gunning_fog)
df['Flesch'] = df['doc'].apply(textstat.flesch_reading_ease)
df.head()
Run
选中Fog、Flesch两列
#查看df[['Fog', 'Smog']]数据类型
type(df[['Fog', 'Flesch']])
Run
pandas.core.frame.DataFrame
对这两个指标水平方向进行均值
df['Mean'] = df[['Fog', 'Flesch']].mean(axis=1)
df.head()
Run
存储到可读性.csv中
df.to_csv('可读性.csv', index=False)
联系客服