【问题标题】:How to calculate number of words in a string in DataFrame? [duplicate]如何计算DataFrame中字符串中的单词数? [复制]
【发布时间】:2016-09-25 19:01:45
【问题描述】:

假设我们有一个简单的数据框

df = pd.DataFrame(['one apple','banana','box of oranges','pile of fruits outside', 'one banana', 'fruits'])
df.columns = ['fruits']

如何计算关键字中的字数,类似于:

1 word: 2
2 words: 2
3 words: 1
4 words: 1

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    IIUC 那么您可以执行以下操作:

    In [89]:
    count = df['fruits'].str.split().apply(len).value_counts()
    count.index = count.index.astype(str) + ' words:'
    count.sort_index(inplace=True)
    count
    
    Out[89]:
    1 words:    2
    2 words:    2
    3 words:    1
    4 words:    1
    Name: fruits, dtype: int64
    

    这里我们使用向量化的str.split 来分割空间,然后apply len 得到元素的数量,然后我们可以调用value_counts 来聚合频率计数。

    然后我们重命名索引并对其进行排序以获得所需的输出

    更新

    这也可以使用 str.len 而不是 apply 来完成,后者应该可以更好地扩展:

    In [41]:
    count = df['fruits'].str.split().str.len()
    count.index = count.index.astype(str) + ' words:'
    count.sort_index(inplace=True)
    count
    
    Out[41]:
    0 words:    2
    1 words:    1
    2 words:    3
    3 words:    4
    4 words:    2
    5 words:    1
    Name: fruits, dtype: int64
    

    时间

    In [42]:
    %timeit df['fruits'].str.split().apply(len).value_counts()
    %timeit df['fruits'].str.split().str.len()
    
    1000 loops, best of 3: 799 µs per loop
    1000 loops, best of 3: 347 µs per loop
    

    对于 6K df:

    In [51]:
    %timeit df['fruits'].str.split().apply(len).value_counts()
    %timeit df['fruits'].str.split().str.len()
    
    100 loops, best of 3: 6.3 ms per loop
    100 loops, best of 3: 6 ms per loop
    

    【讨论】:

    • 起初我很困惑我们怎么能在str.split之后使用str.len,它将每个元素变成一个列表而不是str。所以,我检查了doc 并意识到只要元素是类似列表的类型,该功能就可以工作。希望这个澄清可能对像我这样有同样问题的外行有所帮助。
    【解决方案2】:

    您可以使用str.count 和空格' ' 作为分隔符。

    In [1716]: count = df['fruits'].str.count(' ').add(1).value_counts(sort=False)
    
    In [1717]: count.index = count.index.astype('str') + ' words:'
    
    In [1718]: count
    Out[1718]:
    1 words:    2
    2 words:    2
    3 words:    1
    4 words:    1
    Name: fruits, dtype: int64
    

    时间

    str.count 稍微快一点

    In [1724]: df.shape
    Out[1724]: (6, 1)
    
    In [1725]: %timeit df['fruits'].str.count(' ').add(1).value_counts(sort=False)
    1000 loops, best of 3: 649 µs per loop
    
    In [1726]: %timeit df['fruits'].str.split().apply(len).value_counts()
    1000 loops, best of 3: 840 µs per loop
    

    中等

    In [1728]: df.shape
    Out[1728]: (6000, 1)
    
    In [1729]: %timeit df['fruits'].str.count(' ').add(1).value_counts(sort=False)
    100 loops, best of 3: 6.58 ms per loop
    
    In [1730]: %timeit df['fruits'].str.split().apply(len).value_counts()
    100 loops, best of 3: 6.99 ms per loop
    

    In [1732]: df.shape
    Out[1732]: (60000, 1)
    
    In [1733]: %timeit df['fruits'].str.count(' ').add(1).value_counts(sort=False)
    1 loop, best of 3: 57.6 ms per loop
    
    In [1734]: %timeit df['fruits'].str.split().apply(len).value_counts()
    1 loop, best of 3: 73.8 ms per loop
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 2014-03-03
      相关资源
      最近更新 更多