Reputation: 21
i have some trouble with Pivot (Seem like Pivot in excel). i tried it so many times but i have no idea. Please anyone help me.
Below is a sample data.
Date report Result
01/11/2020 Achieved
01/11/2020 Achieved
02/11/2020 Failed
02/11/2020 Failed
02/11/2020 Achieved
03/11/2020 Achieved
Then result that i need to make it as below
Date report Result
01/11/2020 100.00%
02/11/2020 33.33%
03/11/2020 100.00%
Remark : The percentage result comes from Countif('Date report' and 'Achieved')/Countif('Date report')
Upvotes: 0
Views: 33
Reputation: 862801
Compare values by Achieved
with Series.eq
, aggregate mean
, multiple 100
and convert to percentages:
df1 = (df['Result'].eq('Achieved')
.groupby(df['Date report'])
.mean()
.mul(100)
.round(2)
.astype(str)
.add('%')
.reset_index())
print (df1)
Date report Result
0 01/11/2020 100.0%
1 02/11/2020 33.33%
2 03/11/2020 100.0%
Another idea, thank you @Chester:
df1 = (df['Result'].eq('Achieved')
.groupby(df['Date report'])
.mean()
.map('{:.2%}'.format)
.reset_index())
print (df1)
'{:.num%}'
where num
is precision after the decimal point, read about mini string language
Upvotes: 3