## How to get the maximum number of digits after the decimal point in a Pandas series

pandas remove decimals

df.round not working

pandas describe decimal places

pandas float precision

pandas round to 6 decimal places

pandas print 2 decimal places

pandas limit precision

I read a list of float values of varying precision from a csv file into a Pandas Series and need the number of digits after the decimal point. So, for 123.4567 I want to get 4.

I managed to get the number of digits for randomly generated numbers like this:

```
df = pd.Series(np.random.rand(100)*1000)
precision_digits = (df - df.astype(int)).astype(str).str.split(".", expand=True)[1].str.len().max()
```

However, if I read data from disk using pd.read_csv where some of the rows are empty (and thus filled with nan), I get the following error:
```
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/tgamauf/workspace/mostly-sydan/venv/lib/python3.6/site-packages/pandas/core/generic.py", line 4376, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'str'
```

What is going wrong here? Is there a better way to do what I need?

`pd.read_csv()`

typically returns a `DataFrame`

object. The `StringMethods`

object returned by using `.str`

is only defined for a `Series`

object. Try using `pd.read_csv('your_data.csv' , squeeze=True)`

to have it return a `Series`

object; then you will be able to use `.str`

**pandas.DataFrame.round,** Round a DataFrame to a variable number of decimal places. Additional keywords have no effect but might be accepted for compatibility with numpy. **kwargs. Round a DataFrame to a variable number of decimal places. Parameters decimals int, dict, Series. Number of decimal places to round each column to. If an int is given, round each column to the same number of places. Otherwise dict and Series round to variable numbers of places.

For example you have following data with `NaN`

in it .

df=pd.Series([1.111,2.2,3.33333,np.nan])

idx=df.index# record the original index df=df.dropna()# remove the NaN row (df - df.astype(int)).astype(str).str.split(".", expand=True)[1].str.len().reindex(idx)

**pandas.Series.round,** Number of decimal places to round to. If decimals is negative, it specifies the number of positions to the left of the decimal point. Returns. Series. Rounded These examples show how to use Decimal type in Python and Pandas to maintain more accuracy than float. Pandas can use Decimal, but requires some care to create and maintain Decimal objects. Background - float type can’t store all decimal numbers exactly. For numbers with a decimal separator, by default Python uses float and Pandas uses numpy

The version with `df - df.astype(int)`

does not work correctly for me, simply applying the same `str.split`

without it does:

def get_max_decimal_length(df): """Get the maximum length of the fractional part of the values or None if no values present.""" values = df.dropna() return None if values.empty else values.astype(str).str.split(".", expand=True)[1].str.len().max()

**Python,** decimals : Number of decimal places to round each column to. If an int is given, round each column to the same number of places. Otherwise dict and Series round import math x = 5.55 print((math.floor(x*100)%100)) This is will give you two numbers after the decimal point, 55 from that example. If you need one number you reduce by 10 the above calculations or increase depending on how many numbers you want after the decimal.

**Python,** When doing mathematical operations on series, many times the returned decimals: Int value, specifies upto what number of decimal places the value should be rounded of, default is 0. Hence the Salary column is divided by the Weight column first to get a series with decimal values. variable for max decimal places. An integer with the maximum number of significant digits is (2 113 – 1) · 2 16271. It has ⌊log 10 ((2 113 – 1) · 2 16271)⌋ + 1 = 4,933 digits. Maximum length fraction. A fraction with the maximum number of significant digits is (2 113 – 1) / 2 16494.

**4 Methods to Round Values in Pandas DataFrame,** df['DataFrame column'].round(decimals=number of decimal places needed) Suppose that you have a dataset which contains the following values (with Round each value in a Series to the given number of decimals. Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point. Rounded values of the Series. Round values of an np.array. Round values of a DataFrame.

**Introducing Python: Modern Computing in Simple Packages,** format(n, f, s) l 42 7.030000 string cheese' Same as the preceding example, but the - characters make the rightalignment more the decimal point) still means the number of digits after the decimal for floats, and the maximum Traceback (most recent call last): File "-stolin-", line 1, in -module> ValueFrror: Precision not The maximum precision is 31 digits. All values of a decimal column have the same precision and scale. The range of a decimal variable or the numbers in a decimal column is -n to +n, where n is the largest positive number that can be represented with the applicable precision and scale. The maximum range is 1 - 10³¹ to 10³¹ - 1. DECFLOAT: A decimal floating-point value is an IEEE 754r number with a decimal point. The position of the decimal point is stored in each decimal floating-point value.

##### Comments

- You can fill the missing values beforehand missing
`fillna`

to prevent the mistake from happening, can't you? `squeeze=True`

fixed the problem, but I noticed that it doesn't even apply if I load the whole dataframe and then select a single column (as I need to do anyway). In this case it actually works out of the box. Thank you anyway, as this does answer the question I posed!- What does the
`.reindex(idx)`

do here? The result is the same for my current dataset if I used .max() or .reindex(idx).max().