-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2e1c84
commit 17448c1
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
"""ord_info_function.ipynb | ||
Automatically generated by Colaboratory. | ||
Original file is located at | ||
https://colab.research.google.com/drive/1wXpxeji9dvkf_LuXuxA54BElELaBYg6m | ||
""" | ||
|
||
import pandas as pd | ||
import numpy as np | ||
|
||
indicators = {'not satisfied', 'ok', 'excellent'} | ||
|
||
ds = ['not satisfied', 'not satisfied', 'not satisfied', 'ok', 'ok', 'ok','ok','ok','ok','ok','ok','ok','ok', 'excellent', | ||
'excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent', | ||
'excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent', | ||
'excellent','excellent','excellent','excellent','excellent','excellent','excellent','excellent'] | ||
|
||
def ord_info(ds, indicators): | ||
|
||
# Calculate frequency using pandas value_counts() | ||
freq_counts = pd.value_counts(ds, ascending=True) | ||
freq_counts_df = pd.DataFrame(freq_counts, columns = ['frequency']) | ||
|
||
# Calculate percent | ||
total_responses = len(ds) | ||
freq_counts_df["percent"] = (freq_counts_df["frequency"] / total_responses) * 100 | ||
|
||
# Create DataFrame with all possible indicators | ||
indicators_df = pd.DataFrame(indicators, columns=["indicator"]) | ||
indicators_df.set_index("indicator", inplace=True) | ||
|
||
# Merge the frequency and percent DataFrames to include 0 counts for missing indicators | ||
summary_df = indicators_df.merge(freq_counts_df, how="left", left_index=True, right_index=True) | ||
summary_df.fillna(0, inplace=True) | ||
|
||
# Calculate cumulative percent | ||
summary_df["cumulative"] = summary_df["percent"].cumsum() | ||
|
||
# Set the last cumulative value to 100 | ||
summary_df.loc[summary_df["cumulative"] > 100, "cumulative"] = 100 | ||
|
||
# Create the final DataFrame | ||
summary_df.index.name = "indicators" | ||
return summary_df | ||
|
||
ord_info(ds,indicators) | ||
|