2 回答
TA贡献1895条经验 获得超3个赞
您可以使用以下方法。这个想法是按照这个和这篇文章中所示对月份列进行排序
# Capitalize the month names
df["month"] = df["month"].str.capitalize()
# Correct the spelling of February
df['month'] = df['month'].str.replace('Feburary','February')
# Convert to datetime object for sorting
df['month_in'] = pd.DatetimeIndex(pd.to_datetime(df['month'], format='%B')).month
# Sort using the index
df = df.set_index('month_in').sort_index()
plot_timeline_data(df)
TA贡献1830条经验 获得超3个赞
Dataframe.plot让你的工作更容易一些 - 它将每个系列绘制为不同的线,并保持你指定的顺序:
import matplotlib.pyplot as plt
# Convert the dataframe to series of years
df = df.set_index(["month","year"])["total_sales"].unstack()
# Sort the index (which is month)
df = df.loc[[
"January","feburary","March","April","May","June",
"July", "August", "September","October", "November", "December"
]]
# Plot!
df.plot(marker="o", linestyle="--", rot=90)
# Show all ticks
plt.xticks(range(12), df.index)
添加回答
举报