Issue
So I have a df column which I created by taking an average of three other columns
df['Avg_Grade'] = df.loc[:,'G1':'G3'].mean(axis =1)
The series looks like this (just a sample)
Avg_Grade
0 5.666667
1 5.333333
2 8.333333
3 14.666667
4 8.666667
I'm trying to truncate the output to show something like
0 5.67 (5.66 is also fine)
1 5.33
2 8.33
3 14.67
4 8.67
I've played around with the moduleDecimal
with the following code, but I'm getting an error.
from decimal import *
getcontext().prec = 4
df['Avg_Grade'] = Decimal(df.loc[:,'G1':'G3'].mean(axis =1))
Traceback (most recent call last):
File "<pyshell#409>", line 1, in <module>
df['Avg_Grade'] = Decimal(df.loc[:,'G1':'G3'].mean(axis =1))
File "C:\Python27\lib\decimal.py", line 657, in __new__
raise TypeError("Cannot convert %r to Decimal" % value)
TypeError: Cannot convert 0 5.666667
Solution
There are a few ways you can do this, but they won't work in all situations.
Here's an example dataframe:
In [1]:
df = pd.DataFrame(10*np.random.rand(4,3), columns=['G1','G2','G3'])
df['Avg_Grade'] = df.loc[:,'G1':'G3'].mean(axis =1)
df
Out [1]:
G1 G2 G3 Avg_Grade
0 9.843159 4.155922 9.652694 7.883925
1 2.108822 9.347634 9.271351 6.909269
2 2.681108 3.071449 0.387151 2.046569
3 4.017461 9.464408 0.395482 4.625783
1. Use a global pandas setting
All floats will be displayed with two decimals. You can use either of the following:
pd.options.display.precision = 2
pd.set_option('display.precision', 2)
In [3]: df
Out[3]:
G1 G2 G3 Avg_Grade
0 9.84 4.16 9.65 7.88
1 2.11 9.35 9.27 6.91
2 2.68 3.07 0.39 2.05
3 4.02 9.46 0.40 4.63
2. Use a global setting within a with
statement.
All floats displayed within the with statement will display with two decimals, but after it will revert to the regular value (default:6)
In [4]: with pd.option_context('display.precision', 2):
print(df)
Out[4]:
G1 G2 G3 Avg_Grade
0 9.84 4.16 9.65 7.88
1 2.11 9.35 9.27 6.91
2 2.68 3.07 0.39 2.05
3 4.02 9.46 0.40 4.63
Once you're outside of the with statement:
In [5]: print(df['Avg_Grade'])
0 7.883925
1 6.909269
2 2.046569
3 4.625783
Name: Avg_Grade, dtype: float64
print(df['Avg_Grade'])
3. Using an HTML styler.
This requires you run your code in a Jupyter Notebook.
df.style.set_precision(3)
4. Using round()
If you want to display something, you can also use something like:
df.round(2)
df['Avg_Grade'].round(2)
5. Creating another dataframe or modifying in place
This way will let you customize the precision column by column, but the underlying data is changed, so you might want to do that on a copy.
# Create a copy so we don't mess up the original df
df_print = df.copy()
# Round down some numbers
df_print['Avg_Grade'] = df_print['Avg_Grade'].round(2)
df_print['G1'] = df_print['Avg_Grade'].round(4)
# Add more decimals: need to switch that to a string representation
df_print['G3'] = df_print['G3'].map(lambda x: "{:,.10f}".format(x))
# display
df_print
G1 G2 G3 Avg_Grade
0 7.88 4.155922 9.6526935480 7.88
1 6.91 9.347634 9.2713506079 6.91
2 2.05 3.071449 0.3871511232 2.05
3 4.63 9.464408 0.3954815519 4.63
Answered By - Julien Marrec
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.