python - 如何在python中查找数据框中文本的大小
问题描述
我有一个名为 df 的数据框
它的值为 Text
ID Text
1 Hello, how are you?
2 What time is our meeting tomorrow?
3 It is going to rain soon.
4 nan <----------- Empty record
5 She seems to like me.
如何使用文本大小向数据框中添加新列
ID Text Size
1 Hello, how are you? 19
2 What time is our meeting tomorrow? 35
3 It is going to rain soon. 26
4 nan <----------- Empty record 0
5 She seems to like me. 21
我试过了,但没有用
它现在为文本提供数据框的大小
df["Size"] = len(df["Text"])
解决方案
df['Size'] = df['Text'].str.len()
ID Text Size
0 1 Hello, how are you? 19.0
1 2 What time is our meeting tomorrow? 34.0
2 3 It is going to rain soon. 25.0
3 4
4 5 She seems to like me. 21.0
编辑:
df['Words'] = df['Text'].str.split(r'\s+')
df['Word_count'] = df['Words'].str.len()
ID Text Size Words Word_count
0 1 Hello, how are you? 19.0 ['Hello,', 'how', 'are', 'you?'] 4.0
1 2 What time is our meeting tomorrow? 34.0 ['What', 'time', 'is', 'our', 'meeting', 'tomorrow?'] 6.0
2 3 It is going to rain soon. 25.0 ['It', 'is', 'going', 'to', 'rain', 'soon.'] 6.0
3 4
4 5 She seems to like me. 21.0 ['She', 'seems', 'to', 'like', 'me.'] 5.0
推荐阅读
- angular - Angular - 对文本框的值更改执行过滤器
- c - 尝试从链表中删除时,我丢失了整个列表,而不仅仅是一个 int
- sharepoint - Sharepoint 查找列表
- r - 按字母数字和数字参数聚合列
- google-cloud-platform - 如何使用 cloudarmor 为我的 gke 入口配置 DDoS?
- java - 构建签名APK时android studio中的gradle问题
- html - 我可以在不使用响应式的情况下解决此问题吗?
- angular - RxJs:具有内部可观察的过滤器数组
- vue.js - Vee 验证多重验证
- javascript - 在第一次单击切换后禁用图像元素,然后在反应钩子中使用 setTimeout 重新启用它