1
新手问题,提前谢谢!大熊猫线条图按月划分,按行业划分时间戳SQL导出
我试图按日期和行业对数据进行分组,并显示一个图表,以每月递增的形式显示不同行业收入数字。
我正在从一个SQL导出工作,有时间戳,有一个时间让这个工作。
发布样本CSV数据文件的位置: https://drive.google.com/open?id=0B4xdnV0LFZI1WGRMN3AyU2JERVU
这里有一个小的数据例如:
Industry Date Revenue
Fast Food 01-05-2016 12:18:02 100
Fine Dining 01-08-2016 09:17:48 110
Carnivals 01-18-2016 10:48:52 200
我失败的尝试是在这里:
import pandas as pd
import datetime
import matplotlib.pyplot as plt
df = pd.read_csv('2012_to_12_27_2016.csv')
df['Ship_Date'] = pd.to_datetime(df['Ship_Date'], errors = 'coerce')
df['Year'] = df.Ship_Date.dt.year
df['Ship_Date'] = pd.DatetimeIndex(df.Ship_Date).normalize()
df.index = df['Ship_Date']
df_skinny = df[['Shipment_Piece_Revenue', 'Industry']]
groups = df_skinny[['Shipment_Piece_Revenue', 'Industry']].groupby('Industry')
groups = groups.resample('M').sum()
groups.index = df['Ship_Date']
fig, ax = plt.subplots()
groups.plot(ax=ax, legend=False)
names = [item[0] for item in groups]
ax.legend(ax.lines, names, loc='best')
plt.show()
这非常接近 - 我希望将日期分组为每个行业的月总计并生成一个图表。你知道我能做到吗?谢谢! D – SDS
我编辑了我的解决方案来绘制每月总计,希望它很有用! –