我的数据框 df1 包含以下信息:
Eng Install date Car
0 eng1 3/6/2010 car1
1 eng1 10/25/2010 car1
2 eng1 1/1/2014 car2
3 eng2 1/1/2011 car3
4 eng3 1/1/2014 car1
5 eng4 1/1/2015 car1
6 eng4 1/5/2016 car3
7 eng4 5/8/2017 car2
8 eng5 2/3/2010 car1
9 eng5 1/1/2011 car4
我想绘制条形间隔,显示一个汽车历史记录(选定汽车内的发动机旋转时间范围)
这是car1的示例历史记录:
start end Engine No.
2/3/2010 3/6/2010 eng5
3/6/2010 1/1/2014 eng1
1/1/2014 1/1/2015 eng3
1/1/2015 Today eng4
答案 0 :(得分:0)
这不是你要求的,但希望这足够了:
df = pd.DataFrame({'Eng':['eng1','eng1','eng1','eng2','eng3','eng4','eng4','eng4','eng5','eng5'],
'Install date':['3/6/2010','10/25/2010','1/1/2014','1/1/2011','1/1/2014','1/1/2015','1/5/2016','5/8/2017','2/3/2010','1/1/2011'],
'Car':['car1','car1','car2','car3','car1','car1','car3','car2','car1','car4']})
df
Out[47]:
Eng Install date Car
0 eng1 3/6/2010 car1
1 eng1 10/25/2010 car1
2 eng1 1/1/2014 car2
3 eng2 1/1/2011 car3
4 eng3 1/1/2014 car1
5 eng4 1/1/2015 car1
6 eng4 1/5/2016 car3
7 eng4 5/8/2017 car2
8 eng5 2/3/2010 car1
9 eng5 1/1/2011 car4
df['Install date'] = pd.to_datetime(df['Install date'])
for car in df.Car.unique():
most_recent_eng = df.loc[df.Car == car].groupby('Install date').max().tail(1).Eng.iloc[0]
new_df = df.loc[df.Car == car].append(pd.DataFrame({'Car':[car],'Eng':[most_recent_eng],'Install date':[pd.to_datetime('now')]}), sort=False)
new_df.pivot_table(values='Car',columns='Eng',index='Install date', aggfunc='count').resample('1d').ffill().plot(kind='line',linewidth=10, title=car)
结果:
答案 1 :(得分:0)
这是有点不同的方法。对于标签,请使用ax.annotate。我将标签定为日期,但不知道您是否要使用这些日期作为标签或发动机编号。我确定您是从这里得到的:
import pandas as pd
df = pd.DataFrame({'Eng':['eng1','eng1','eng1','eng2','eng3','eng4','eng4','eng4','eng5','eng5'],
'Install date':['3/6/2010','10/25/2010','1/1/2014','1/1/2011','1/1/2014','1/1/2015','1/5/2016','5/8/2017','2/3/2010','1/1/2011'],
'Car':['car1','car1','car2','car3','car1','car1','car3','car2','car1','car4']})
# df
# Out[47]:
# Eng Install date Car
# 0 eng1 3/6/2010 car1
# 1 eng1 10/25/2010 car1
# 2 eng1 1/1/2014 car2
# 3 eng2 1/1/2011 car3
# 4 eng3 1/1/2014 car1
# 5 eng4 1/1/2015 car1
# 6 eng4 1/5/2016 car3
# 7 eng4 5/8/2017 car2
# 8 eng5 2/3/2010 car1
# 9 eng5 1/1/2011 car4
df['Install date'] = pd.to_datetime(df['Install date'])
for car in df.Car.unique():
most_recent_eng = df.loc[df.Car == car].groupby('Install date').max().tail(1).Eng.iloc[0]
new_df = df.loc[df.Car == car].append(pd.DataFrame({'Car':[car],'Eng':[most_recent_eng],'Install date':[pd.to_datetime('now')]}), sort=False)
piv = new_df.pivot_table(values='Car',columns='Eng',index='Install date', aggfunc='count')
piv = piv.reindex(columns=piv.ffill().sum().sort_values(ascending=False).index).ffill()
ax = piv.plot(marker='o', linestyle='-',linewidth=2, title=car, ms=4)
j = 0.005
i = 1
for date, eng in zip(new_df[:-1]['Install date'].tolist(),new_df[:-1]['Eng'].tolist()):
ax.annotate(date.strftime("%Y-%m-%d"), (date, 1.00), xycoords='data',xytext=(date, 1.00+i*j), textcoords='data',
arrowprops=dict(arrowstyle="->"),
)
j+=0.005
i = -1*i
制作: