Search code examples
pythoncount

Counting commas in two columns and showing the count side by side


Based on the sample dataframe below how can I get the comma count of two columns?

Sample data:

 ID  City       Zipcode
  1   A,B,C,D    1,2,3,4
  2   A,B        1,2
  3   A          1
  4   B,C        2,3

Desired output:

City       Zipcode  City_Count Zipcode_Count
A,B,C,D    1,2,3,4  4          4
A,B        1,2      2          2
A          1        1          1
B,C        2,3      2          2

Code:

# Based on my research so far, below is what I have found to count values/commas in a single column

df['value_count'] = df.string_column.str.count(',')

print (df)

Solution

  • You can use:

    df[['City_count', 'Zipcode_count']] = df[['City', 'Zipcode']].map(lambda x: x.count(','))
    

    to give:

          City  Zipcode  City_count  Zipcode_count
    0  A,B,C,D  1,2,3,4           3              3
    1      A,B      1,2           1              1
    2        A        1           0              0
    3      B,C      2,3           1              1
    

    I do not understand why your comma-count is 1 greater than the numnber of commas