1

I have this dataframe: dataframe

In excel I have the simple count if =if((COUNTIFS(B:B,B2,A:A,"=A"))=0,"No","Yes")

So essentially, how do I loop through all of the rows, countif the Cell in MatchID against Range(MatchID) AND if Range(provider) = "A".

Whilst recording the entry in a new column.

Simply enough to do in VBA and excel but python/Pandas is new to me and still slightly beyond my mental grasp.

Is this possible?

Thanks for any help.

rkta
  • 3,959
  • 7
  • 25
  • 37
  • 3
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jan 30 '18 at 13:08
  • Also, not everyone is familiar with Excel - users may not know what that Excel formula is accomplishing, you may want to explain what it is doing so it can be replicated in `pandas` – MattR Jan 30 '18 at 13:29

3 Answers3

0

Here is a vectorised solution. In general, try and use a set for comparisons and in-built pandas functionality such as map / isin. This not only is more efficient, but more readable.

filter_set = set(df.loc[df['Provider']=='A', 'MatchID'])  # set of MatchIDs with Provider 'A'

df['Solution'] = df['MatchID'].isin(filter_set).map({True: 'Yes', False: 'No'})
jpp
  • 159,742
  • 34
  • 281
  • 339
0

This code will help.

#create dataframe object
#consider it as df

check = (df['MatchID'] > 'yourmatchId') & (a['provider'] <"A")
df["NewColumn"] = df[check]['YourRequiredColumn']
Arun
  • 31
  • 3
-1

You can go about that like this:

cond_list = df.loc[df['Provider']=='A', 'MatchID'].tolist() #list of MatchIDs that have Provider with value 'A'

df['Solution'] = df['MatchID'].apply(lambda x: 'Yes' if x in cond_list else 'No')
zipa
  • 27,316
  • 6
  • 40
  • 58