Format certain floating dataframe columns into percentage in pandas

user3576212 :

I am trying to write a paper in IPython notebook, but encountered some issues with display format. Say I have following dataframe df, is there any way to format var1 and var2 into 2 digit decimals and var3 into percentages.

       var1        var2         var3    
id                                              
0    1.458315    1.500092   -0.005709   
1    1.576704    1.608445   -0.005122    
2    1.629253    1.652577   -0.004754    
3    1.669331    1.685456   -0.003525   
4    1.705139    1.712096   -0.003134   
5    1.740447    1.741961   -0.001223   
6    1.775980    1.770801   -0.001723    
7    1.812037    1.799327   -0.002013    
8    1.853130    1.822982   -0.001396    
9    1.943985    1.868401    0.005732

The numbers inside are not multiplied by 100, e.g. -0.0057=-0.57%.

Woody Pride :

replace the values using the round function, and format the string representation of the percentage numbers:

df['var2'] = pd.Series([round(val, 2) for val in df['var2']], index = df.index)
df['var3'] = pd.Series(["{0:.2f}%".format(val * 100) for val in df['var3']], index = df.index)

The round function rounds a floating point number to the number of decimal places provided as second argument to the function.

String formatting allows you to represent the numbers as you wish. You can change the number of decimal places shown by changing the number before the f.

p.s. I was not sure if your 'percentage' numbers had already been multiplied by 100. If they have then clearly you will want to change the number of decimals displayed, and remove the hundred multiplication.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Changing certain values in multiple columns of a pandas DataFrame at once

Format certain floating dataframe columns into percentage in pandas

Keep certain columns in a pandas DataFrame, deleting everything else

Keep certain columns in a pandas DataFrame, deleting everything else

Stack columns in pandas dataframe to achieve record format

Python Pandas: pivot only certain columns in the DataFrame while keeping others

Percentage match in pandas Dataframe

Creating new pandas dataframe from certain columns of existing dataframe

How to exclude certain columns of a pandas dataframe?

Drop percentage of a dataframe [pandas]

Scale floating values in selected columns in pandas dataframe to between 0 and 1

Pandas Dataframe: Update values in a certain columns for last n rows

Drop columns and rows with certain percentage of 0's pandas

Reorder certain columns in pandas dataframe

Extract rows where the lists of columns contain certain values in a pandas dataframe

Pandas DataFrame mean of data in columns occurring before certain date time

Sum of only certain columns in a pandas Dataframe

Pandas not converting certain columns of dataframe to datetimeindex

How to work out percentage of total with groupby for specific columns in a pandas dataframe?

how to calculate the percentage in a group of columns in pandas dataframe while keeping the original format of data

How to get percentage count based on multiple columns in pandas dataframe?

Pandas DataFrame Combine Certain Columns in Nested JSON

Apply function to certain groups of columns of a pandas dataframe

How to keep certain columns when unstack multilevel pandas dataframe

Finding percentage proportion using specific columns and rows of pandas dataframe

Color only certain rows and columns of a Pandas DataFrame

Verifying the data format in columns for a pandas dataframe

Pandas percentage of two columns

Merge certain columns of a pandas dataframe with data from another dataframe by condition