I want to round up the numbers but it seems not to work. Style.format is not actually doing anything to the data. And if I save it to CSV or send it to Excel the numbers are not rounded.
import pandas as pd
df = pd.DataFrame(
data={
"Ticker": {0: 'AAA', 1: 'BBB'},
"High": {0: 111.23456, 1: 22.23789},
"Low": {0: 333.7894, 1: 33.456},
"Close": {0: 444.03030, 1: 44.09840},
"Volume": {0: 555555.111, 1: 6988.040}
}
)
format_mapping = {"High": "{:.2f}", "Low": "{:.2f}", "Close": "{:.2f}", "Volume": "{:.0f}"}
df.style.format(format_mapping)
# df = df.style.format(format_mapping)
# print(df)
In jupyther notebook the line "df.style.format(format_mapping)" it working for presenting but actually does not do it. If I use the last line, the numbers are not rounded.
Why? I just want a simple code to round up numbers in a data frame.
This is actually working:
df = df.round({"High":2, "Low":2, "Close":2, "Volume":0})
print(df)
Result:
Ticker High Low Close Volume
0 AAA 111.23 333.79 444.03 555555.0
1 BBB 22.24 33.46 44.10 6988.0
Except for the 'volume' . But this can be solved by adding a new line :
df['Volume'] = df['Volume'].astype(int)
Not that nice, but it works.