I'm new to Python programming. Currently I try to make a graph that able to show percentage on top of bar chart in 2 decimal places.
df_survey is dataframe that i made from using pandas library. (I try to copy datafame df_survey_sort into df_survey_pct but when i make change in df_survey_pct, df_survey_sort also change... can someone explain to me why this happen. As result, I do as following to make df_survey_sort and df_survey_pct not overwite on each other)
df_survey = df_survey[['Very interested','Somewhat interested','Not interested']]
df_survey_sort = df_survey.sort_values(by='Very interested', ascending=0)
#df_survey_pct = df_survey_sort
df_survey_pct = df_survey.sort_values(by='Very interested', ascending=0)
total_ds = df_survey_sort.sum(axis=1)
for i in range(0,df_survey_sort.shape[0]):
df_survey_pct.iloc[i][0] = round(df_survey_sort.iloc[i][0]/total_ds[i]*100,2)
df_survey_pct.iloc[i][1] = round(df_survey_sort.iloc[i][1]/total_ds[i]*100,2)
df_survey_pct.iloc[i][2] = round(df_survey_sort.iloc[i][2]/total_ds[i]*100,2)
this is the datatype of df_survey_pct
Very interested int64
Somewhat interested int64
Not interested int64
dtype: object
when I execute print(df_survey_pct)
, the value of each cell is not in decimal places.
I even try df_survey_pct = df_survey_pct.round(2)
and df_survey_pct = df_survey_pct.astype('float')
however the value is still in integer.
Because of this, I can only show integer percentage in my bar chart.