Categories
Python Answers

How to know if a generator is empty from the start with Python?

Sometimes, we want to know if a generator is empty from the start with Python.

In this article, we’ll look at how to know if a generator is empty from the start with Python.

How to know if a generator is empty from the start with Python?

To know if a generator is empty from the start with Python, we can call next to see if the StopIteration exception is raised.

For instance, we write

def peek(iterable):
    try:
        first = next(iterable)
    except StopIteration:
        return None
    return first, itertools.chain([first], iterable)

res = peek(my_sequence)
if res is None:
    # ...
else:
    # ...

to define the peek function.

In it, we call next with iterable to see if the first item is returned or the StopIteration exception is raised.

If the error is raised, we return None.

Otherwise, we return first and the iterator that we get by calling chain with [first] and iterable which no longer has the first object.

Then we call peek to see is the returned result is None to see if the generator is empty.

Conclusion

To know if a generator is empty from the start with Python, we can call next to see if the StopIteration exception is raised.

Categories
Python Answers

How to fix TypeError: ‘<=' not supported between instances of 'str' and 'int' with Python?

Sometimes, we want to fix TypeError: ‘<=’ not supported between instances of ‘str’ and ‘int’ with Python.

In this article, we’ll look at how to fix TypeError: ‘<=’ not supported between instances of ‘str’ and ‘int’ with Python

How to fix TypeError: ‘<=’ not supported between instances of ‘str’ and ‘int’ with Python?

To fix TypeError: ‘<=’ not supported between instances of ‘str’ and ‘int’ with Python, we should convert the strings to ints.

For instance, we write

vote = int(input("Enter your message"))

if (0 <= vote <= 24):
    # ...

to call input with the prompt to get the value of vote.

We convert the input value to an int with int before we compare the vote value with other ints.

Conclusion

To fix TypeError: ‘<=’ not supported between instances of ‘str’ and ‘int’ with Python, we should convert the strings to ints.

Categories
Python Answers

How to convert the string 2.90K to 2900 or 5.2M to 5200000 in a Python Pandas dataframe?

Sometimes, we want to convert the string 2.90K to 2900 or 5.2M to 5200000 in a Python Pandas dataframe.

In this article, we’ll look at how to convert the string 2.90K to 2900 or 5.2M to 5200000 in a Python Pandas dataframe.

How to convert the string 2.90K to 2900 or 5.2M to 5200000 in a Python Pandas dataframe?

To convert the string 2.90K to 2900 or 5.2M to 5200000 in a Python Pandas dataframe, we call replace with a dict to map the patterns to new substrings and the regex argument set to True.

For instance, we write

df["Val"].replace({"K": "*1e3", "M": "*1e6"}, regex=True)

to replace the values in the Val column.

We replace the substrings in the keys with the substrings in the values by calling replace with the dict and regex set to True.

Conclusion

To convert the string 2.90K to 2900 or 5.2M to 5200000 in a Python Pandas dataframe, we call replace with a dict to map the patterns to new substrings and the regex argument set to True.

Categories
Python Answers

How to do locale date formatting in Python?

Sometimes, we want to do locale date formatting in Python.

In this article, we’ll look at how to do locale date formatting in Python.

How to do locale date formatting in Python?

To do locale date formatting in Python, we can use the babel package.

To install it, we run

pip install Babel

Then we use it by writing

from datetime import date, datetime, time
from babel.dates import format_date, format_datetime, format_time

d = date(2020, 4, 1)
fd = format_date(d, locale='en')

to call format_date with date d and locale set to 'en' to return a date string that’s formatted for the English locale.

Conclusion

To do locale date formatting in Python, we can use the babel package.

Categories
Python Answers

How to skip rows during csv import with Python Pandas?

Sometimes, we want to skip rows during csv import with Python Pandas.

In this article, we’ll look at how to skip rows during csv import with Python Pandas.

How to skip rows during csv import with Python Pandas?

To skip rows during csv import with Python Pandas, we can call read_csv with the skiprows argument.

For instance, we write

import pandas as pd
from StringIO import StringIO

s = """1, 2
3, 4
5, 6"""

df = pd.read_csv(StringIO(s), skiprows=[1], header=None)

to call read_csv with the StringIO object with csv string s.

We set skiprows to [1] to skip the 2nd row.

And we set header to None to skip parsing the header.

Conclusion

To skip rows during csv import with Python Pandas, we can call read_csv with the skiprows argument.