![](http://image109.360doc.com/DownloadImg/2022/10/2308/254541896_2_20221023081820100_wm.png)
textstat可以对文本进行可读性计算,支持英文、德语、西班牙、意大利、荷兰语等。目前不支持中文,如做中文文本分析,可以考虑用cntext包。之前分享过两篇可读性的内容 https://github.com/textstat/textstat 今天尝试将pandas和textstat结合起来,对csv做文本分析
任务今天在本文中,将学习三个知识点。 - 选中csv中某列文本数据,依次使用apply方法,计算FOG、ARI、CLI等。
安装pip3 install textstat
读取数据import pandas as pd import textstat #设置dataframe显示的宽度 pd.options.display.max_colwidth = 100
df = pd.read_csv('data.csv') df
Run ![](http://image109.360doc.com/DownloadImg/2022/10/2308/254541896_3_20221023081820397_wm.png)
Series批操作使用apply方法对pd.Series类型的数据进行批操作 extstat库有丰富的可读性方法,这里任选2个作为 批操作函数。 - Fog textstat.gunning_fog(text)
- Flesch textstat.flesch_reading_ease(text)
df['Fog'] = df['doc'].apply(textstat.gunning_fog) df['Flesch'] = df['doc'].apply(textstat.flesch_reading_ease)
df.head()
Run ![](http://image109.360doc.com/DownloadImg/2022/10/2308/254541896_4_20221023081820522_wm.png)
DataFrame均值选中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 ![](http://image109.360doc.com/DownloadImg/2022/10/2308/254541896_5_20221023081820632_wm.png)
存储存储到可读性.csv中 df.to_csv('可读性.csv', index=False)
精选文章
|