如何计算串联对象中的模式字符串?

时间:2017-08-28 01:44:56

标签: python pandas

假设这样的数据:

>>> data
                x
0   [wdq, sda, q]
1    [q, d, qasd]
2  [d, b, sdaaaa]

我想知道每个列表中有多少字符串包含a,这意味着我需要这样的答案:

>>> data
                x  count_a
0   [wdq, sda, q]        1
1    [q, d, qasd]        1
2  [d, b, sdaaaa]        1

我怎么能在python中做到这一点?

4 个答案:

答案 0 :(得分:2)

假设这是pandas.DataFramexlist对象:

df['count_a'] = df['x'].apply(lambda x: sum('a' in e for e in x))

答案 1 :(得分:1)

你可以试试这个;

for i in my_series:
    print (i.count('a'))

这给你的每个系列字母

答案 2 :(得分:0)

要了解每个String中包含字母List的{​​{1}}个数量,您可以使用以下内容:

a

这将打印以下内容:

l = ['wdq', 'sda', 'qaaa']

print(sum([1 for x in l if 'a' in x]))

答案 3 :(得分:0)

a = ['a', '12asf3', 'sdf']
b = ['gfdg5', '   ', 'vag gfd4']
c = [' fd4 ', 'sfsa fa', 'df4 a']

abc = [a, b, c]

for mem in abc:
  counter = 0
  for str in mem:
    if 'a' in str:
      counter += 1
  print abc.index(mem), mem, counter

输出:

0 ['a', '12asf3', 'sdf'] 2
1 ['gfdg5', '   ', 'vag gfd4'] 1
2 [' fd4 ', 'sfsa fa', 'df4 a'] 2