I am trying to round off a data frame column value but the round-off is not getting applied. Below is my code. I am multiplying that particular value first by 100 and then rounding it off.
treasury_shares.loc[:,'%Stake'] *= 100
treasury_shares['%Stake'].apply(np.floor)
In my data frame, I am getting values that are still not rounded off. I want to round off in the manner for e.g. if the value is 32.8 then 33 should be shown and if the value is 32.2 then 32 should be shown. Also, I then want to concatenate each rounded value in the data frame column %Stake with % as for example '32%'
CodePudding user response:
Kindly try with the following:
treasure_shares['%Stake'] = round(treasure_shares * 100,0).astype(str) "%"
First multiplying by 100, followed by rounding with 0 decimals places, and lastly converted to string so we can concatenate "%" as text.
CodePudding user response:
df = pd.DataFrame({'%Stake':[0.3,0.2,0.1,0.4,0.5]})
print(df)
###
%Stake
0 0.3
1 0.2
2 0.1
3 0.4
4 0.5
df['%Stake'] = df['%Stake'].astype(float).map("{:.0%}".format)
print(df)
###
%Stake
0 30%
1 20%
2 10%
3 40%
4 50%
CodePudding user response:
The apply
function returns the result rather than modifying your dataframe. Reassign the column values like you did when multiplying by 100
treasury_shares.loc[:,'%Stake'] = treasury_shares['%Stake'].apply(np.floor)