Reputation: 255
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.
Upvotes: 0
Views: 3176
Reputation: 1196
Here is how you convert np.float64 columns to 2 decimal places
df_survey["some_column_with_too_many_decimal"] = df_survey["some_column_with_too_many_decimal"].apply(lambda x: int(x*100)/100)
Also to select only certain rows in that column if that is what you need, please use df.loc
instead of iloc on every row, since the df might have too many rows.
df.loc[(df["column1"]>0), ["column2", "column3"]]
or
df.loc[(df["column1"]>0), "column2", "column3"]
The first argument to loc is a list of conditions to filter by, the second argument is the columns to select and then you can update them by using apply as show above.
If you want to use round, you can round off values then multiply by 100, convert to int and divide by 100 making it decimal with 2 places. The round function does not limit it to 2 decimal places because of the way the values are stored in the dataframe.
Upvotes: 1