2

I have an array that looks like this:

a = ['UCI_99648;102568',  'UCI_99648;102568',  'UCI_99648;102568;99651', 'UCI_99651', 'UCI_99652', 'SIB_99658;102568;506010;706080', NaN]

I want to find out how many stings have a single number like UCI_99651, UCI_99652

So, the expected outcome is 2.

How I can do this in python.

NOTE: my actual data is very large and the numbers can be anything and, as stated in the example, may involve missing values.

msh855
  • 1,493
  • 1
  • 15
  • 36
  • what makes UCI_99651, UCI_99652 unique numbers? – Ora Aff Apr 25 '19 at 09:40
  • by unique here I mean there is only one number in the string compared to other cases. For example, the code `UCI_99648;102568` has two numbers. – msh855 Apr 25 '19 at 10:41

4 Answers4

4

Assuming that the structure of all strings follows that of the example above, a list comprehension as the following will do:

l = ['UCI_99648;102568',  'UCI_99648;102568',  'UCI_99648;102568;99651', 
     'UCI_99651', 'UCI_99652', 'SIB_99658;102568;506010;706080', 'NaN']

[i for i in l if ';' not in i and i != 'NaN']

Output

['UCI_99651', 'UCI_99652']
yatu
  • 86,083
  • 12
  • 84
  • 139
2

Since you have tagged pandas,another way:

s=pd.Series(a).dropna()
s[s.str.split(';').str.len().eq(1)]

3    UCI_99651
4    UCI_99652
anky
  • 74,114
  • 11
  • 41
  • 70
2

You can try like below.Hope this will address your problem.

p = [word.split(";")[0] for word in uci if word != 'NaN']
print(Counter(p))
#Counter({'UCI_99648': 3, 'UCI_99651': 1, 'UCI_99652': 1, 'SIB_99658': 1})
#To filter only one occurance you can try below.
b = [word for word in p if p.count(word)==1]
print(b)

For more you can refer the list comprehension doc here.

http://dataunbox.com/course/24/119/Python%20for%20dummies

Amaresh
  • 3,231
  • 7
  • 37
  • 60
0

You can extract numbers using regular expressions. For example, something like this:

import re
import numpy as np
from collections import Counter


def count_strings_with_unq_nums(list_of_strings):

    # Initialize two lists - one to keep track of where the numbers occur and another to isolate unique occurences of numbers
    all_nums_nested = []
    all_nums_flat = []

    # Loop through all strings and extract integers within
    for s in list_of_strings:
        try:
            nums = re.findall(r'\d+', s)
            all_nums_nested.append(nums)
            all_nums_flat.extend(nums)
        except:
            continue

    # Count occurences of all extracted numbers
    num_counter = Counter(all_nums_flat)

    # Loop through nested list to find strings where unique numbers occur
    unq_items = []
    for key, val in num_counter.items():
        if val == 1:
            for n, num_list in enumerate(all_nums_nested):
                if key in num_list:
                    unq_items.append(list_of_strings[n])

    # Return the number of strings containing unique numbers.        
    return len(set(unq_items))

if __name__ == '__main__':
    a = ['UCI_99648;102568',  'UCI_99648;102568',  'UCI_99648;102568;99651', 'UCI_99651', 'UCI_99652', 'SIB_99658;102568;506010;706080', np.NaN]

    print(count_strings_with_unq_nums(a))

>>> 2
berkelem
  • 2,005
  • 3
  • 18
  • 36