I have a DataFrame with an index, and then a reference to other indexes, and an organization. For example:
import pandas as pd
inp = [{'index' :1, 'refIndex':3, 'org' : 'org1'}, {'index':2, 'refIndex':1, 'org': 'org1'}, {'index':3, 'refIndex': 2, 'org' : 'org2'}]
df = pd.DataFrame(inp)
print df
Output:
index refIndex org
0 1 3 org1
1 2 1 org1
2 3 2 org2
What I need to do is count on each row how many other rows where that row's index occurs as the refIndex from the same org.
So I end up with a DataFrame like:
index refIndex org count
0 1 3 org1 1 # index 1 org1 occurs as refIndex and org once elsewhere
1 2 1 org1 0 # index 2 org1 occurs as refIndex and org nowhere else
2 3 2 org2 0 # index 3 org2 occurs as refIndex and org nowhere else
I am new to Python and Pandas, so please excuse if this is obvious to you. I have been struggling all day with trying groupbys, functions, for loops inside for loops, merges.