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:\Users\ma\Desktop\filee') 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
CodePudding user response:
This way you can match the list value with the column value of df, and don't use the name list for list, give it some other name. You can't loop through entire df, you can loop through columns by this method. There are better ways to do this though, but this is the simplest.
count=0
for x in list:
for y in df.column.tolist():
if x==y:
count =1
``
CodePudding user response:
Firstly, welcome to StackOverflow. Check the guidelines for how to properly ask a question (there is no question in your post). I think I understand what you want though.
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)