TypeError: unsupported format string passed to Series.__format__
I am trying to add a thousand separator to my dash cards and all my efforts have been futile. This solution is found to be more accurate but it gives me an error
TypeError: unsupported format string passed to Series.format
@app.callback(
[Output('sls', 'children'),
Output('wngs', 'children'),
Output('rvne', 'children')],
Input('year', 'value')
)
def card_update(select_year):
dff=df_good.copy()
df_formattedd=dff.groupby(['year'], as_index=False)[['Net Sale', 'Winnings','Revenue']].sum()
df_formattedd[['Net Sale','Winnings','Revenue']]=df_formattedd[['Net Sale','Winnings','Revenue']].apply(lambda x:round(x,2))
df_formattedd[['Net Sale','Winnings','Revenue']]= df_formattedd[['Net Sale','Winnings','Revenue']].apply(lambda x: f'{x:,}')
sales=df_formattedd[df_formattedd['year']==select_year]['Net Sale']
winnings=df_formattedd[df_formattedd['year']==select_year]['Winnings']
revenue=df_formattedd[df_formattedd['year']==select_year]['Revenue']
return sales, winnings, revenue
Short abstracted example code that reproduces your problem:
import pandas as pd
df = pd.DataFrame(
{
"A": [1000000.111, 1000000.222, 1000000.333],
"B": [2000000.111, 2000000.222, 2000000.333],
"C": [3000000.111, 3000000.222, 3000000.333],
}
)
df[["A", "B", "C"]] = df[["A", "B", "C"]].apply(lambda x: round(x, 2))
df[["A", "B", "C"]] = df[["A", "B", "C"]].apply(lambda x: f'{x:,}')
gives:
TypeError: unsupported format string passed to Series.format
The problem is with the last line where you try to format the numbers.
The problem is that x
in the lambda function refers to a Pandas Series and not a number. A Series doesn't support the type of formatting string you're using:
The ',' option signals the use of a comma for a thousands separator.
https://docs.python.org/3/library/string.html
Instead you could do something like this:
import pandas as pd
df = pd.DataFrame(
{
"A": [1000000.111, 1000000.222, 1000000.333],
"B": [2000000.111, 2000000.222, 2000000.333],
"C": [3000000.111, 3000000.222, 3000000.333],
}
)
df[["A", "B", "C"]] = df[["A", "B", "C"]].apply(lambda x: round(x, 2))
df[["A", "B", "C"]] = df[["A", "B", "C"]].apply(
lambda series: series.apply(lambda value: f"{value:,}")
)
So the idea behind the nested apply
here is: For each column in the DataFrame df[["A", "B", "C"]]
take each row value and apply the string formatter to it. The row values are just floats so the string formatter is able to handle that.
Result
>>> print(df)
A B C
0 1,000,000.11 2,000,000.11 3,000,000.11
1 1,000,000.22 2,000,000.22 3,000,000.22
2 1,000,000.33 2,000,000.33 3,000,000.33
Alternatively you can set the format using pd.options.display.float_format
:
pd.options.display.float_format = "{:,}".format
Keep in mind that this is applies to more than just df[["A", "B", "C"]]
.