Trying to get one column of a df into a list, but a KeyError is thrown each time.
Writing a function to convert a dataframe to a subplot. I want to take the first and last value in the df column containing x-axis values so that I can label the xaxis of the subplot with the first and last values only. tolist() (or to_list) is throwing a KeyError, however. Can anyone point out where i'm going wrong?
def df_to_subplot(df, df_field_x_axis, subplot_title, nrows, ncols):
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, sharex=True, sharey=True)
df.set_index(df_field_x_axis, inplace=True)
for col, ax in zip(df.columns, axes.flat):
if col != df_field_x_axis:
df[col].plot.line(ax=ax)
ax.set_title(col)
df = df.sort_values(df_field_x_axis)
x_ticks = df[df_field_x_axis].tolist() <<<<<------throws a KeyError
for ax in axes.flatten():
ticks = ax.get_xticks()
ax.set_xticks(x_ticks[0], x_ticks[-1])
ax.set_yticks()
plt.setp(ax.get_xticklabels(), rotation=0)
The KeyError is thrown because you're trying to access a column that doesn't exist in the DataFrame. This is because you've set df_field_x_axis as the index of the DataFrame with df.set_index(df_field_x_axis, inplace=True). After this operation, df_field_x_axis is no longer a column in the DataFrame, but the index.
To fix this, you should get the index values to a list instead of trying to access it as a column. Here's the corrected code:
def df_to_subplot(df, df_field_x_axis, subplot_title, nrows, ncols):
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, sharex=True, sharey=True)
df.set_index(df_field_x_axis, inplace=True)
for col, ax in zip(df.columns, axes.flat):
if col != df_field_x_axis:
df[col].plot.line(ax=ax)
ax.set_title(col)
df = df.sort_index()
x_ticks = df.index.tolist() # <<<<<------changed this line
for ax in axes.flatten():
ticks = ax.get_xticks()
ax.set_xticks([x_ticks[0], x_ticks[-1]]) # <<<<<------changed this line
ax.set_yticks()
plt.setp(ax.get_xticklabels(), rotation=0)
In the line ax.set_xticks([x_ticks[0], x_ticks[-1]]), I've changed x_ticks[0], x_ticks[-1] to [x_ticks[0], x_ticks[-1]] because set_xticks expects a list of x locations for the ticks.
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html