【问题标题】:Get a unique list of strings in pandas after a split() operation在 split() 操作后获取 pandas 中唯一的字符串列表
【发布时间】:2014-04-20 12:16:46
【问题描述】:

我开始使用 pandas,并且在较大的 DataFrame 中有 一列数据,例如

0                  one two
1            two seven six
2           three one five
3    seven five five eight
4                 six four
5                    three
dtype: object

我想做的是将单词序列拆分为它们的组成部分,然后为单词获取一个唯一的集合或计数。我可以很好地进行拆分

numbers.str.split(' ')

0                    [one, two]
1             [two, seven, six]
2            [three, one, five]
3    [seven, five, five, eight]
4                   [six, four]
5                       [three]
dtype: object

但是,我不确定从这里去哪里。同样,我想要输出,例如

['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']

或在带有计数的字典中相同,或在这两者之一的 Series/DataFrame 等效项中。

到目前为止,我能做的最好的事情就是将 apply() 与 Set 结合使用来获取唯一的单词。到目前为止,pandas 是一个非常优雅的包,对于比我更了解它的人来说,这似乎是触手可及的。

提前致谢!

【问题讨论】:

标签: python split pandas unique


【解决方案1】:

如果我理解正确,我认为您可以使用 pandas 进行如下操作。在拆分字符串之前,我将从系列开始:

print s

0                  one two
1            two seven six
2           three one five
3    seven five five eight
4                 six four
5                    three

stacked = pd.DataFrame(s.str.split().tolist()).stack()
print stacked

0  0      one
   1      two
1  0      two
   1    seven
   2      six
2  0    three
   1      one
   2     five
3  0    seven
   1     five
   2     five
   3    eight
4  0      six
   1     four
5  0    three

现在只需计算系列的值计数:

print stacked.value_counts()

five     3
one      2
three    2
six      2
two      2
seven    2
eight    1
four     1
dtype: int64

【讨论】:

    【解决方案2】:

    此代码将您的所有单词及其计数制作成字典。

    x = ['one two', 'two seven six', 'three one five', 'seven five five eight', 'six four', 'three']
    
    #create list comprehension of all elements
    x_list = [j for i in x for j in i.split()]
    print x_list
    
    # ['one', 'two', 'two', 'seven', 'six', 'three', 'one', 'five', 'seven', 'five', 'five', 'eight', 'six', 'four', 'three']
    
    d = {}
    
    #initialize keys
    for e in set(x_list):
        d[e] = 0
    
    #store counts in dict
    for e in x_list:
            d[e] += 1
    
    print d
    

    结果是一个带有计数的字典:

    {'seven': 2, 'six': 2, 'three': 2, 'two': 2, 'four': 1, 'five': 3, 'eight': 1, 'one': 2}
    

    【讨论】:

    • 感谢您的加入。我认为您对我的示例数据感到厌烦,因此我编辑了问题以澄清。我正在 Pandas API 中寻找答案。
    【解决方案3】:

    我最近正在处理类似的任务,我想计算空格分隔的字符串。将它用于您的数据将是这样的:

    import pandas as pd
    data = [['one two'],['two seven six'],['three one five'],['seven five five eight'],['six four'],['three']]
    numbers = pd.DataFrame(data)
    
    uniq_groups = set(x for l in numbers[0].str.split(' ') for x in l)
    #{'eight', 'five', 'four', 'one', 'seven', 'six', 'three', 'two'}
    
    #add a dataframe column for count of each value
    for gr in uniq_groups:
       numbers[gr] = numbers[0].map(lambda x: len([i for i in x.split(' ') if i == gr]))
    
    #sum all columns
    numbers.loc['Total'] = numbers.sum(axis=0,numeric_only=True)
    #pandas display format without decimals
    pd.options.display.float_format = '{:,.0f}'.format
    

    导致:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 2017-06-30
      • 2023-03-16
      相关资源
      最近更新 更多