Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)
OpenAssistant Conversations (oasst2)
I would like to create a python format string to only print 2 decimal places when the…
In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
- User
I would like to create a python format string to only print 2 decimal places when the number is less than 10 or just 1 digit. For larger numbers i dont care about the 2 decimal places. Can you help me with that?
Here is some starter code that makes some example data:
import pandas as pd format_logic = '${:,.2f}' pd.options.display.float_format = format_logic.format df = pd.DataFrame([123.4567, 0.4678, 1.6789, 456.7890], index=['foo','bar','baz','quux'], columns=['cost']) print(df)How can i change
format_logicto only show the 2 decimal places for low numbers and ignore it for bigger numbers where i dont need the 2 decimal place precision? - Assistant
Here is whole example again, working as you need:
import pandas as pd def my_float_format(float_number:float)-> str: """Format floating number in my custom style""" if float_number >10: return '${:,.0f}'.format(float_number) else: return '${:,.2f}'.format(float_number) pd.options.display.float_format = my_float_format f = pd.DataFrame([123.4567, 0.4678, 1.6789, 456.7890], index=['foo','bar','baz','quux'], columns=['cost']) print(df) - User
That did it, thanks!
- Assistant
You're welcome, glad I could help!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →