Scientific notation is a way of expressing numbers using exponents, which can be useful for very large or very small numbers, but can also make data difficult to read and interpret. Fortunately, Pandas provides a way to suppress scientific notation and display numbers in standard decimal format.
Here are some ways to suppress scientific notation in Pandas:
Using pandas.set_option():
You can use the set_option() method to set the display format of pandas to display numbers in standard decimal format instead of scientific notation. Here is an example code:
import pandas as pd pd.set_option('display.float_format', lambda x: '%.3f' % x) # Sample data data = pd.DataFrame({'col1': [123456789, 0.0000000123456], 'col2': [987654321, 0.0000000098765]}) # Display the data without scientific notation print(data)
This will display the data in the data DataFrame without scientific notation:
col1 col2 0 123456789.000 987654321.000 1 0.000 0.000
Using pandas.DataFrame.round():
You can also use the round() method to round the values in your DataFrame to a certain number of decimal places. Here is an example code:
import pandas as pd # Sample data data = pd.DataFrame({'col1': [123456789, 0.0000000123456], 'col2': [987654321, 0.0000000098765]}) # Round the data to 3 decimal places data = data.round(3) # Display the data without scientific notation print(data)
This will display the data in the data DataFrame without scientific notation:
col1 col2 0 123456789.000 987654321.000 1 0.000 0.000
Using pandas.DataFrame.apply():
You can also use the apply() method to apply a formatting function to all the values in your DataFrame. Here is an example code:
import pandas as pd # Sample data data = pd.DataFrame({'col1': [123456789, 0.0000000123456], 'col2': [987654321, 0.0000000098765]}) # Define a formatting function def format_func(x): return '{:.3f}'.format(x) # Apply the formatting function to all the values in the DataFrame data = data.applymap(format_func) # Display the data without scientific notation print(data)
This will display the data in the data DataFrame without scientific notation:
col1 col2 0 123456789.000 987654321.000 1 0.000 0.000
Using pandas.options.display.float_format:
You can also use the pandas.options.display.float_format attribute to set the default formatting for floats. Here is an example code:
import pandas as pd # Set the default float formatting to suppress scientific notation pd.options.display.float_format = '{:.3f}'.format # Sample data data = pd.DataFrame({'col1': [123456789, 0.0000000123456], 'col2': [987654321, 0.0000000098765]}) # Display the data without scientific notation print(data)
This will display the data in the data DataFrame without scientific notation:
col1 col2 0 123456789.000 987654321.000 1 0.000 0.000
Note that using the pd.options.display.float_format attribute will affect the formatting of all floats in your code, not just the ones in your DataFrame.
In conclusion, there are several ways to suppress scientific notation in Pandas, including using set_option(), round(), apply(), and pd.options.display.float_format. You can choose the method that best fits your use case and preferences.