Sea of Tranquility    About    Archive    Feed

Random pandas notes

A dump of pandas functions which I have found useful. Use with discretion.

import matplotlib.pyplot as plt
import pandas a pd

fig = plt.figure(figsize=(6,6)) # Create a Figure object
ax = fig.subplots() # Create an AxesSubplot object
#Both Figure and AxesSubplot are classes defined in matplotlib

#Plot CO2 on y-axis and index along x-axis.
df['CO2'].plot(kind='line', color='b', ax=ax)

#If two columns are being plotted, then we should speficy both of them
df.plot(x='x-column', y='y-column', kind='line', color='b', ax=ax)

ax.set(title='Title', xlabel='XLabel', ylabel='Ylabel')
ax.legend().set_visible(False) #Remove legend
ax.legend(loc="upper left")

#Rotate x-tick labels. For datetime objects see below
for tick in ax.get_xticklabels():
	tick.set_rotation(45)

#Formatting datetime tick labels
fig.autofmt_xdate()

fig.savefig('file.png', transparent=False, dpi=300, bbox_inches='tight')
plt.show()


You can either set it to True or the column whose values muct be interpreted as datetime objects.

By default it is set to False.

df = pd.read_csv("filename.csv", parse_dates=["Column"])
df = pd.read_csv("filename.csv", parse_dates=True)