count list values that appears in dataFrame using python

Question:

I want to count list value that is exists in dataframe:

I want to use a loop to go through list values and dataframe df and if list[0] exist in df count++.

my code:
df = pd.read_excel(‘C:UsersmaDesktopfilee’)
df looks like this :

Intents Examples
First something
Second something

listX= ["HOFF", "Customers", "bank"]

I did this but not working:

count = 0
for i in listX[0]:
for x in df['Examples']:
    if x.str.contains(i):
       count++
Asked By: Maha Mohammed

||

Answers:

With the below mthod you can match the list value with the column value of your dataframe.

Don’t use the name list for your list, give it some other name.

You can’t loop through the entire dataframe, so use the code below to loop through columns instead.

There are better ways to do this though, but the below is one way.

count=0
for x in list:
    for y in df.column.tolist():
         if x==y:
             count+=1
``
Answered By: Sardar Arslan

In general, you don’t want to use loops with pandas. Its not the way the API was intended to be used. You would probably want to use pandas lambda functions since they are easy to understand and will get you started on the right path. The following will check to see if items in ListX match the items in the Example column. Keep in mind, I am assuming the datatype of the Example column is a list and not a string.

listX= ["HOFF", "Customers", "bank"]

# define the function that will give you the desired output
def count_items(examples):
    matching_items = [examples.index(x) for x in listX]
    return len(matching_items)

# then execute the function you defined with a lambda expression
# you pass in the values you wish to pass into the function
# axis = 1 means you want to loop over rows not columns
df['Count'] = df.apply(lambda x: count_items(x["Examples"]), axis = 1)

If you need to split the string value of the example column to a list, use the .split() function:

s = '1,2,3'
list = s.split(',')
print(list)    # prints ['1', '2', '3']

You can add this logic into your count_items function, before matching the items. This way the value is in the correct datatype (list)

Answered By: Ibn Masood
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.