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.

Categories
Python Answers

How to fix googletrans stopped working with error ‘NoneType’ object has no attribute ‘group’ with Python?

Sometimes, we want to fix googletrans stopped working with error ‘NoneType’ object has no attribute ‘group’ with Python.

In this article, we’ll look at how to fix googletrans stopped working with error ‘NoneType’ object has no attribute ‘group’ with Python.

How to fix googletrans stopped working with error ‘NoneType’ object has no attribute ‘group’ with Python?

To fix googletrans stopped working with error ‘NoneType’ object has no attribute ‘group’ with Python, we can install the google_trans_new package.

To install it, we run

pip install google_trans_new

Then we use it by writing

from google_trans_new import google_translator

translator = google_translator()
translate_text = translator.translate("Hola!", lang_src="es", lang_tgt="en")
print(translate_text)

to call translator.translate with the text we want to translate.

We set the lang_src to the source language.

And lang_tgt is set to the language we want to translate to.

We get the translated text from translated_text.

Conclusion

To fix googletrans stopped working with error ‘NoneType’ object has no attribute ‘group’ with Python, we can install the google_trans_new package.