Categories
Python Answers

How to change date format in Python Pandas bar plot?

Sometimes, we want to change date format in Python Pandas bar plot, we use strftime.

In this article, we’ll look at how to change date format in Python Pandas bar plot, we use strftime.

How to change date format in Python Pandas bar plot?

To change date format in Python Pandas bar plot, we use strftime.

For instance, we write

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.ticker as ticker

start = pd.to_datetime("5-1-2012")
idx = pd.date_range(start, periods= 365)
df = pd.DataFrame({'A':np.random.random(365), 'B':np.random.random(365)})
df.index = idx
df_ts = df.resample('W', how= 'max')

ax = df_ts.plot(kind='bar', x=df_ts.index, stacked=True)

ticklabels = ['']*len(df_ts.index)
ticklabels[::4] = [item.strftime('%b %d') for item in df_ts.index[::4]]
ticklabels[::12] = [item.strftime('%b %d\n%Y') for item in df_ts.index[::12]]
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.gcf().autofmt_xdate()

plt.show()

We call item.strftime with the date format to format the item datetime into the date time string we want.

%b is the abbreviated month name like Jan, Feb, etc,

%d is the day of the month as a 2 digit number.

%Y is the 4 digit year number.

We have

ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))

to set the axes formatter.

And we call autofmt_xdate to format the dates.

Conclusion

To change date format in Python Pandas bar plot, we use strftime.

Categories
Python Answers

How to change one value based on another value in Python Pandas?

Sometimes, we want to change one value based on another value in Python Pandas.

In this article, we’ll look at how to change one value based on another value in Python Pandas.

How to change one value based on another value in Python Pandas?

To change one value based on another value in Python Pandas, we can use loc to get the rows and columns we want to assign the new values to.

For instance, we write

import pandas
df = pandas.read_csv("test.csv")
df.loc[df.ID == 103, 'FirstName'] = "Matt"
df.loc[df.ID == 103, 'LastName'] = "Jones"

to get the rows and columns with ID value set to 103 and column FirstName and LastName and set them to new values.

We get the values with df.loc.

Conclusion

To change one value based on another value in Python Pandas, we can use loc to get the rows and columns we want to assign the new values to.

Categories
Python Answers

How to convert string to datetime format in Pandas Python?

Sometimes, we want to convert string to datetime format in Pandas Python.

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

How to convert string to datetime format in Pandas Python?

To convert string to datetime format in Pandas Python, we can use the to_datetime method.

For instance, we write

pd.to_datetime(df['I_DATE'])

to call to_datetime on the I_DATE column to convert the values in the I_DATE column to datetimes.

Conlusion

To convert string to datetime format in Pandas Python, we can use the to_datetime method.

Categories
Python Answers

How to use groupby results to dictionary of lists with Python Pandas?

Sometimes, we want to use groupby results to dictionary of lists with Python Pandas.

In this article, we’ll look at how to use groupby results to dictionary of lists with Python Pandas.

How to use groupby results to dictionary of lists with Python Pandas?

To use groupby results to dictionary of lists with Python Pandas, we can us the apply and to_dict methods.

For instance, we write

df.groupby('Column1')['Column3'].apply(list).to_dict()

to call groupby with 'Column1 to group the df data frame values by Column1.

And then we get the values from Column3 from the grouped results.

And then we call apply with list to convert the result to a list.

Finally, we call to_dict to convert the dict to a dictionary.

Conclusion

To use groupby results to dictionary of lists with Python Pandas, we can us the apply and to_dict methods.

Categories
Python Answers

How to use Python Pandas to read in table without headers?

Sometimes, we want to use Python Pandas to read in table without headers.

In this article, we’ll look at how to use Python Pandas to read in table without headers.

How to use Python Pandas to read in table without headers?

To use Python Pandas to read in table without headers, we can use the read_csv method with the header argument set to None.

For instance, we write

df = pd.read_csv(file_path, header=None, usecols=[3,6])

to call read_csv with the CSV file_path and header set to None to skip the header when reading the CSV.

Conclusion

To use Python Pandas to read in table without headers, we can use the read_csv method with the header argument set to None.