I have the following dataframe with pairs of strings in tuples
d = {'value': [['Red', 'Blue'],
['Blue', 'Yellow'],
['Blue', 'Yellow'],
['Yellow', 'Orange'],
['Green', 'Purple'],
['Purple', 'Yellow'],
['Yellow', 'Red'],
['Violet', 'Blue'],
['Blue', 'Green'],
['Green', 'Red'],
['Red', 'Brown'],
['Blue', 'Green']]}
df = pd.DataFrame(data = d)
And I want to find for each row probability, which can be calculated based on number of rows with same values
def find_prob(df, tup):
d = df[df.new.apply(lambda x: x[0] == tup[0] and x[1] == tup[1])].shape[0]
p = df[df.new.apply(lambda x: x[0] == tup[0])].shape[0]
return d / p
df['probs'] = df.new.apply(lambda x: find_prob(df, x))
I know it's dumb to pass DataFrame in apply function so I want to known if there's a way to improve this logic
Desired output is:
P.S. I want to divide number of rows on number of rows, that start with first value of a tuple
