Categories
Python Answers

How to get the parent directory in Python?

Sometimes, we want to get the parent directory in Python.

In this article, we’ll look at how to get the parent directory in Python.

How to get the parent directory in Python?

To get the parent directory in Python, we can use the pathlib module.

For instance, we write

from pathlib import Path

path = Path("/here/your/path/file.txt")
print(path.parent.absolute())

to create a Path object with the path.

Then we get the parent directory with path.parent.

And we call absolute to get the absolute path of the parent directory.

Conclusion

To get the parent directory in Python, we can use the pathlib module.

Categories
Python Answers

How to calculate frequency counts for unique values in an array with Python NumPy?

Sometimes, we want to calculate frequency counts for unique values in an array with Python NumPy.

In this article, we’ll look at how to calculate frequency counts for unique values in an array with Python NumPy.

How to calculate frequency counts for unique values in an array with Python NumPy?

To calculate frequency counts for unique values in an array with Python NumPy, we can use the np.unique method with the return_counts argument set to True.

For instance, we write

import numpy as np

x = np.array([1,1,1,2,2,2,5,25,1,1])
unique, counts = np.unique(x, return_counts=True)
a = np.asarray((unique, counts)).T

to call np.unique with array x and return_counts set to True.

Then we combine the unique values and its counts into a single array with nested arrays that has the unique value with its count inside.

Conclusion

To calculate frequency counts for unique values in an array with Python NumPy, we can use the np.unique method with the return_counts argument set to True.

Categories
Python Answers

How to calculate the angle between a line and the horizontal axis with Python?

Sometimes, we want to calculate the angle between a line and the horizontal axis with Python.

In this article, we’ll look at how to calculate the angle between a line and the horizontal axis with Python.

How to calculate the angle between a line and the horizontal axis with Python?

To calculate the angle between a line and the horizontal axis with Python, we calculate the difference between the x and y coordinates.

Then we use atan2 with the deltas to get the angle.

For instance, we write

delta_y = P2_y - P1_y
delta_x = P2_x - P1_x
angle_degrees = atan2(delta_y, delta_x) * 180 / PI

to calculate the delta_y and delta_x values from the by subtracting the coordinates of y and x of the 2 points.

Then we call atan2 with delta_y and delta_x to get the angle between the line and the horizontal axis in radians.

Then we multiply that by 180 / PI` to get the value in degrees.

Concllusion

To calculate the angle between a line and the horizontal axis with Python, we calculate the difference between the x and y coordinates.

Then we use atan2 with the deltas to get the angle.

Categories
Python Answers

How to make a Python script to do something at the same time every day?

Sometimes, we want to make a Python script to do something at the same time every day.

In this article, we’ll look at how to make a Python script to do something at the same time every day.

How to make a Python script to do something at the same time every day?

To make a Python script to do something at the same time every day, we can use the schedule module.

To install it, we run

pip install schedule

Then we use it by writing

import schedule
import time

def job(t):
    print('working', t)
    return

schedule.every().day.at("01:00").do(job, 'It is 01:00')

while True:
    schedule.run_pending()
    time.sleep(60)

to create an infinite while loop that calls schedule.run_pending to run the job function every day at 01:00.

We specify the schedule of the job with

schedule.every().day.at("01:00").do(job, 'It is 01:00')

We call every and use the day property to run the job every day.

And we call at with '01:00' to run the job at 01:00.

Conclusion

To make a Python script to do something at the same time every day, we can use the schedule module.

Categories
Python Answers

How to automatically read dates from a CSV file with Python Pandas?

Sometimes, we want to automatically read dates from a CSV file with Python Pandas.

In this article, we’ll look at how to automatically read dates from a CSV file with Python Pandas.

How to automatically read dates from a CSV file with Python Pandas?

To automatically read dates from a CSV file with Python Pandas, we can set the date_parser argument.

For instance, we write

from datetime import datetime
dateparse = lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S')

df = pd.read_csv(infile, parse_dates=['datetime'], date_parser=dateparse)

to call read_csv with the file to read.

And we set parse_dates to 'datetime' to parse dates with datetime.

Then we specify the date_parser argument by setting it to the dateparse functuon we created to parse the dates with the given format.

Conclusion

To automatically read dates from a CSV file with Python Pandas, we can set the date_parser argument.