Categories
Python Answers

How to change the datetime format in Python Pandas?

Sometimes, we want to change the datetime format in Python Pandas.

In this article, we’ll look at how to change the datetime format in Python Pandas.

How to change the datetime format in Python Pandas?

To change the datetime format in Python Pandas, we can use the datetime’s strftime method.

For instance, we write

df['date'] = pd.to_datetime(df["date"].dt.strftime('%Y-%m'))

to call to_datetime with the formatted dates that we get with

df["date"].dt.strftime('%Y-%m')

to convert the date column’s to a different datetime format.

%Y is the 4 digit year and %m is the 2 digit month.

Conclusion

To change the datetime format in Python Pandas, we can use the datetime’s strftime method.

Categories
Python Answers

How to convert list of dictionaries to a Python Pandas DataFrame?

Sometimes, we want to convert list of dictionaries to a Python Pandas DataFrame.

In this article, we’ll look at how to convert list of dictionaries to a Python Pandas DataFrame.

How to convert list of dictionaries to a Python Pandas DataFrame?

To convert list of dictionaries to a Python Pandas DataFrame, we can use the DataFrame class.

For instance, we write

df = pd.DataFrame(d)

to create a data frame df by passing the list of dicts d into DataFrame directly.

Conclusion

To convert list of dictionaries to a Python Pandas DataFrame, we can use the DataFrame class.

Categories
Python Answers

How to change the “tick frequency” on x or y axis in Python matplotlib?

Sometimes, we want to change the "tick frequency" on x or y axis in Python matplotlib.

In this article, we’ll look at how to change the "tick frequency" on x or y axis in Python matplotlib.

How to change the "tick frequency" on x or y axis in Python matplotlib?

To change the "tick frequency" on x or y axis in Python matplotlib, we can use the xticks method.

For instance, we write

import numpy as np
import matplotlib.pyplot as plt

x = [0, 5, 9, 10, 15]
y = [0, 1, 2, 3, 4]
plt.plot(x, y)
plt.xticks(np.arange(min(x), max(x) + 1, 1.0))
plt.show()

to call plt.xticts with np.arange(min(x), max(x) + 1, 1.0) to space the ticks by differ by 1 and render the values from the lowest value in x to the highest value in x plus 1.

Conclusion

To change the "tick frequency" on x or y axis in Python matplotlib, we can use the xticks method.

Categories
Python Answers

How to iterate a list as pair (current, next) in Python?

Sometimes, we want to iterate a list as pair (current, next) in Python.

In this article, we’ll look at how to iterate a list as pair (current, next) in Python.

How to iterate a list as pair (current, next) in Python?

To iterate a list as pair (current, next) in Python, we can use the itertools.tee method.

For instance, we write

import itertools
def pairwise(iterable):
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b)   

to call itertools.tee with the iterable object to return an iterator with the tuples that has the current and next item values each in their own lists.

Then we call next to get the values.

And then we call zip with a and b to combine them into a list with tuples containing the current and next item values and return it.

Conclusion

To iterate a list as pair (current, next) in Python, we can use the itertools.tee method.

Categories
Python Answers

How to find unique rows in a Python numpy.array?

Sometimes, we want to find unique rows in a Python numpy.array.

In this article, we’ll look at how to find unique rows in a Python numpy.array.

How to find unique rows in a Python numpy.array?

To find unique rows in a Python numpy.array, we can use the unique method.

For instance, we write

unique_rows = np.unique(original_array, axis=0)

to call np.unique with the origina_array NumPy array.

We specify that we check for uniques on axis 0.

Conclusion

To find unique rows in a Python numpy.array, we can use the unique method.