Categories
Python Answers

How to format output string with right alignment with Python?

Sometimes, we want to format output string with right alignment with Python.

In this article, we’ll look at how to format output string with right alignment with Python.

How to format output string with right alignment with Python?

To format output string with right alignment with Python, we can use the string format method.

For instance, we write

line_new = '{:>12}  {:>12}  {:>12}'.format(word[0], word[1], word[2])

to call '{:>12} {:>12} {:>12}'.format with the 3 items to put in place of the placeholders.

The placeholders specify that the width of each column is 12 characters and that the alignment of each item is right.

Conclusion

To format output string with right alignment with Python, we can use the string format method.

Categories
Python Answers

How to execute Python script via crontab?

Sometimes, we want to execute Python script via crontab.

In this article, we’ll look at how to execute Python script via crontab.

How to execute Python script via crontab?

To execute Python script via crontab, we can edit the crontab file to add an entry to run the script.

For instance, we add

*/10 * * * * /usr/bin/python script.py

to run script.py every 10 minutes.

Conclusion

To execute Python script via crontab, we can edit the crontab file to add an entry to run the script.

Categories
Python Answers

How to remove stop words using nltk or Python?

Sometimes, we want to remove stop words using nltk or Python

In this article, we’ll look at how to remove stop words using nltk or Python.

How to remove stop words using nltk or Python?

To remove stop words using nltk or Python, we can use the stopwords.words list from nltk.

For instance, we write

from nltk.corpus import stopwords
# ...
filtered_words = [word for word in word_list if word not in stopwords.words('english')]

to get English stop words with stopwords.words('english').

Then we return a list of words in word_list that aren’t stop words with

word for word in word_list if word not in stopwords.words('english')

Conclusion

To remove stop words using nltk or Python, we can use the stopwords.words list from nltk.

Categories
Python Answers

How to simply cache function return values with Python?

Sometimes, we want to simply cache function return values with Python.

In this article, we’ll look at how to simply cache function return values with Python.

How to simply cache function return values with Python?

To simply cache function return values with Python, we can use the lru_cache decorator.

For instance, we write

from functools import lru_cache

@lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

to create the fib fibonacci sequence function.

And we use the lru_cache decorator on it to cache the returned values of fib.

Conclusion

To simply cache function return values with Python, we can use the lru_cache decorator.

Categories
Python Answers

How to replace blank values (white space) with NaN in Python Pandas?

Sometimes, we want to replace blank values (white space) with NaN in Python Pandas.

In this article, we’ll look at how to replace blank values (white space) with NaN in Python Pandas.

How to replace blank values (white space) with NaN in Python Pandas?

To replace blank values (white space) with NaN in Python Pandas, we can call replace on the data frame.

For instance, we write

df = pd.DataFrame([
    [-0.532681, 'foo', 0],
    [1.490752, 'bar', 1],
    [-1.387326, 'foo', 2],
    [0.814772, 'baz', ' '],     
    [-0.222552, '   ', 4],
    [-1.176781,  'qux', '  '],         
], columns='A B C'.split(), index=pd.date_range('2000-01-01','2000-01-06'))

print(df.replace(r'^\s*$', np.nan, regex=True))

to create the df` data frame.

Then we replace all whitespace values with NaN by call replace with the regex to match whitespaces, np.nan and regex set to True.

Conclusion

To replace blank values (white space) with NaN in Python Pandas, we can call replace on the data frame.