0

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')

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Similar idea, df['Result'].eq('Achieved').groupby(df['Date report']).mean().map('{:.2%}'.format) but leveraging on mini string language
Added few dets, feel free to change if not ok. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.