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.

Categories
Python Answers

How to execute raw SQL in Flask-SQLAlchemy app with Python?

Sometimes, we want to execute raw SQL in Flask-SQLAlchemy app with Python.

In this article, we’ll look at how to execute raw SQL in Flask-SQLAlchemy app with Python.

How to execute raw SQL in Flask-SQLAlchemy app with Python?

To execute raw SQL in Flask-SQLAlchemy app with Python, we can use the db.session.execute method.

For instance, we write

result = db.session.execute('SELECT * FROM my_table WHERE my_column = :val', {'val': 5})

to call db.session.execute with the SQL string and dict with the values to fill for the placeholders.

We get the result returned from the select statement returned with execute.

Conclusion

To execute raw SQL in Flask-SQLAlchemy app with Python, we can use the db.session.execute method.