'exclude specific text-based values from comma separated value count and give output excluding those

I want to count the ; separated values in a dataframe

solution for that can be something like this

count=[] 
for row in gdf.itertuples():
    newstr = row.info.split(";")
    n = len(newstr)
    count.append(n)

gdf["count"] = count 

However here is an issue i dont want to count those ; seperated values if they are a specific symbol, in my case its either #, ##, ### or #### so lets say for image below for id:2 i want the count to be two and for id:6 i want the count to be one and so on

enter image description here

failed attempts so far

  • i tried stripping them before doing the count, i replaced using .replace method and # symbols were removed but the separators still stayed and made it more messier
  • tried len-1 didnt worked
  • i tried adding another for loop and if statement which i thought would work
    but no
count=[]                            
for row in gdf.itertuples():
        newstr = row.info.split(";")
        for i in newstr:
            if (i !='#'):
                n = len(newstr)
        count.append(n)

thanks for the help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source