×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Hi, just an easy question here f2

## Hi, just an easy question here f

(OP)
Hi,

just an easy question here from a beginner:)

How do I find out the value_count of X which has a condition?

For example

df['Drinks'].contains("diet").value_counts()

### RE: Hi, just an easy question here f

you can try something like this:

#### CODE

>>> def count_of_elements_containing_pattern(list_of_strings, pattern):
return [pattern in string for string in list_of_strings].count(True)

>>> my_list = ["foo", "bar", "baz", "spam", "eggs", "foobar"]
>>> count_of_elements_containing_pattern(my_list, "foobar")
1
>>> count_of_elements_containing_pattern(my_list, "foo")
2
>>> count_of_elements_containing_pattern(my_list, "bar")
2
>>> count_of_elements_containing_pattern(my_list, "a")
4
>>> count_of_elements_containing_pattern(my_list, "z")
1
>>> count_of_elements_containing_pattern(my_list, "oo")
2 

### RE: Hi, just an easy question here f

You are forcing us to guess that df might be a pandas dataframe.

value_counts() returns a series with the count of each unique value in a column.

Then find the thing you are interested in within that series and look up the count.

### RE: Hi, just an easy question here f

Out of curiosity i installed pandas and tried the previous example with it:

#### CODE

>>> import pandas as pd
>>> my_list = ["foo", "bar", "baz", "spam", "eggs", "foobar"]
>>> my_series=pd.Series(my_list)
>>> my_series[my_series.str.match("foobar")].count()
1
>>> my_series[my_series.str.match("foo")].count()
2
>>> my_series[my_series.str.match(".*bar")].count()
2
>>> my_series[my_series.str.match(".*a.*")].count()
4
>>> my_series[my_series.str.match(".*z.*")].count()
1
>>> my_series[my_series.str.match(".*oo.*")].count()
2 

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!