TypeError: 'int' object is not iterable with zip()

the question goes like this: revenue = [30000, 50000, 70000, 90000] cost = [10000, 15000, 20000, 30000] Write a comprehension together with the zip() function to make a new list that maintains profits. I tried doing it with for-loop: new_list = [] for revenue, cost in zip(revenue, cost): profit = revenue – cost new_list.append(profit) new_list… Read More TypeError: 'int' object is not iterable with zip()

Compare two DataFrame columns of lists of strings (A & B) to find if any in B are NOT in A for adding to Col C

d = {‘Col A’: [[‘Singapore’,’Germany’,’UK’],[‘Ireland’,’Japan’,’Australia’],[‘India’,’Korea’,’Vietnam’]], ‘Col B’: [[‘Singapore’,’Germany’,’UK’],[‘Ireland’,’Japan’],[‘India’,’Mexico’,’Argentina’]]} df = pd.DataFrame(data=d) I’m trying to compare these two columns and return a new column, Col C, that contains any strings that are present in Col B but NOT present in Col A. So row 1 being the same returns no value, row 2 where A contains… Read More Compare two DataFrame columns of lists of strings (A & B) to find if any in B are NOT in A for adding to Col C

How to substitute a list of words based on a list of tuples by preserving the same original order?

Given the following list of tokens: a = [‘heyyo’, ‘how’, ‘ale’, ‘yiou’] And a list of tuples: b = [(‘yiou’, ‘you’), (‘heyyo’, ‘hello’)] How can I replace the elements of the list a considering the elements of the list b? For example, the expected output would be: [‘hello’, ‘how’, ‘ale’, ‘you’] This is due to… Read More How to substitute a list of words based on a list of tuples by preserving the same original order?

Set specific rows values to column in pandas dataframe using list comprehension

I would like to change values in a column for specific rows. The rows are defined in a vector containing the row indexes. I thought this could be done with list comprehension (similar to apply function in R). I tried this: [(dataframe.loc[(dataframe[‘id’]== x), ‘column_to_be_changed’] = 1) (x) for x in indexes] The error message is… Read More Set specific rows values to column in pandas dataframe using list comprehension

Search DataFrame column for words in list

I am trying to create a new DataFrame column that contains words that match between a list of keywords and strings in a df column… data = { ‘Sandwich Opinions’:[‘Roast beef is overrated’,’Toasted bread is always best’,’Hot sandwiches are better than cold’] } df = pd.DataFrame(data) keywords = [‘bread’, ‘bologna’, ‘toast’, ‘sandwich’] df[‘Matches’] = [df.apply(lambda… Read More Search DataFrame column for words in list