Here is a DataFrame where I use the groupby()
function to obtain the number of positive and negative occurrences per day.
data = [['28-08-22', 'Positive'], ['28-08-22', 'Negative'], ['28-08-22', 'Positive'], ['28-08-22', 'Positive'], ['27-08-22', 'Positive'], ['27-08-22', 'Negative'], ['27-08-22', 'Negative'], ['27-08-22', 'Negative']]
df = pd.DataFrame(data, columns=['Date', 'actions'])
df2 = df.groupby('Date')['actions'].value_counts(normalize=True)
print(df2)
I would like to calculate the ratio of those occurrences for each day, meaning dividing positive value / negative value
for each day.
Also, if on a day the positive value is higher than the negative one, I would like the ratio to be positive. If the negative value is higher than the positive, I would like the ratio to be negative.
I am new to python so definitely need your help. Thank you!
CodePudding user response:
Try to do the following instead of calculation df2:
df = df.pivot_table(index='Date',columns='actions',aggfunc='size').reset_index().set_index('Date')
df['percent'] = df['Negative'] / df.sum(axis=1)