×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*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.

Students Click Here

Hi, just an easy question here f
2

Hi, just an easy question here f

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()

Thanks in advance

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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close