首页 > 解决方案 > 如何在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"])

标签: python

解决方案


pandas.Series.str.len

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

推荐阅读