Categories
Python Answers

How to truncate float values with Python?

Sometimes, we want to truncate float values with Python.

In this article, we’ll look at how to truncate float values with Python.

How to truncate float values with Python?

To truncate float values with Python, we can use the round function.

For instance, we write:

n = round(1.923328437452, 3)
print(n)

to call round with the number to round and the number of decimal places to round to respectively.

And then we assign the rounded number to n.

Therefore, n is 1.923.

Conclusion

To truncate float values with Python, we can use the round function.

Categories
Python Answers

How to insert a list into a cell with Python Pandas?

Sometimes, we want to insert a list into a cell with Python Pandas.

In this article, we’ll look at how to insert a list into a cell with Python Pandas.

How to insert a list into a cell with Python Pandas?

To insert a list into a cell with Python Pandas, we can assign the list we want to insert to the location in the data frame we want to insert it in.

For instance, we write:

import pandas as pd

df = pd.DataFrame(data={'A': [1, 2, 3], 'B': ['x', 'y', 'z']})

df.at[1, 'B'] = ['c', 'd']

We create a data frame by using the DataFrame class with a dictionary.

Then we assign the list ['c', 'd'] into the location [1, 'B'] with:

df.at[1, 'B'] = ['c', 'd']

Therefore, df is now:

   A       B
0  1       x
1  2  [c, d]
2  3       z

Conclusion

To insert a list into a cell with Python Pandas, we can assign the list we want to insert to the location in the data frame we want to insert it in.

Categories
Python Answers

How to display a decimal in scientific notation with Python?

Sometimes, we want to display a decimal in scientific notation with Python.

In this article, we’ll look at how to display a decimal in scientific notation with Python.

How to display a decimal in scientific notation with Python?

To display a decimal in scientific notation with Python, we can use the %.2E format string.

For instance, we write:

from decimal import Decimal

print('%.2E' % Decimal('40800000000.00000000000000'))

We create a decimal number object with the Decimal constructor and a number string.

Then we use %.2E to return a string with the number formatted into scientific notation.

Therefore, print prints '4.08E+10'.

Conclusion

To display a decimal in scientific notation with Python, we can use the %.2E format string.

Categories
Python Answers

How to check if a float value is a whole number with Python?

Sometimes, we want to check if a float value is a whole number with Python.

In this article, we’ll look at how to check if a float value is a whole number with Python.

How to check if a float value is a whole number with Python?

To check if a float value is a whole number with Python, we can use the float’s is_integer method.

For instance, we write:

print((1.0).is_integer())
print((1.5).is_integer())

We call is_integer on 2 floats and print out the values.

Since 1.0 is an integer, the first print will print True.

And since 1.5 isn’t an integer, the 2nd print will print False.

Conclusion

To check if a float value is a whole number with Python, we can use the float’s is_integer method.

Categories
Python Answers

How to create nice column output in Python?

Sometimes, we want to create nice column output in Python.

In this article, we’ll look at how to create nice column output in Python.

How to create nice column output in Python?

To create nice column output in Python, we can use format strings with print.

For instance, we write:

table_data = [['a', 'b', 'c'], ['aa', 'b', 'c'], ['a', 'bb', 'c']]
for row in table_data:
    print("{: >20} {: >20} {: >20}".format(*row))

We use the {: >20} to set each column to have at least 20 characters and align the text to the right.

Therefore, we get:

                   a                    b                    c
                  aa                    b                    c
                   a                   bb                    c

from the print output.

Conclusion

To create nice column output in Python, we can use format strings with print.