I want each values of df have different decimal point like this
year month day
count 1234 5678 9101
mean 12.12 34.34 2.3456
std 12.12 3.456 7.789
I searched to find a way to change specific value's decimal point but couldn't find the way. So this is what I've got
year month day
count 1234.0000 5678.0000 9101.0000
mean 12.1200 34.3400 2.3456
std 12.1200 3.4560 7.7890
I know the round() method but I don't know how to assign it to each values not the whole row or columns. Is it possible to change values separately?
CodePudding user response:
You can change displayning of floats:
pd.options.display.float_format = '{:,6f}'.format
#if necessary convert to floats
df = df.astype(float)
Or change format to 6 zeros:
df = df.astype(float).applymap('{:.6f}'.format)
CodePudding user response:
The format
approach is correct, but I think what you are looking for is this:
Input file data.txt
year month day
count 1234.0000 5678.0000 9101.0000
mean 12.1200 34.3400 2.3456
std 12.1200 3.4560 7.7890
Formatting (see formatting mini language)
import numpy as np
import pandas as pd
file = "/path/to/data.txt"
df = pd.read_csv(file, delim_whitespace=True)
# update all columns with data type number
# use the "n" format
df.update(df.select_dtypes(include=np.number).applymap('{:n}'.format))
print(df)
Output
year month day
count 1234 5678 9101
mean 12.12 34.34 2.3456
std 12.12 3.456 7.789