我有一个具有以下结构的数据框:
custid province year features... label
123 AB 2005 ... 0
124 ON 2006 ... 1
...
999 QC 2012 ... 1
最后一列是标签/目标。
我有一个lambda函数:
def churn_per_feature(x):
d = {}
d['churn_count'] = (x['label'] == 1).sum()
d['cust_count'] = x['custid'].nunique()
d['churn_rate'] = d['churn_count'] / float(d['cust_count'])
return pd.Series(d, index = ['churn_count', 'cust_count', 'churn_rate'])
我对两个变量province
和year
进行了分组:
churn_per_province_year = df.groupby(['province', 'year']).apply(churn_per_feature)
我正在尝试pyplot
,一个带有线的单个图表,其中x轴是years
,每条线代表province
(到目前为止,我仅选择4客户人数最多的省份,因此不会出现循环):
plt.plot(years, churn_per_province_year[churn_per_province_year['province'] == 'ON']['cust_count'])
plt.plot(years, churn_per_province_year[churn_per_province_year['province'] == 'AB']['cust_count'])
plt.plot(years, churn_per_province_year[churn_per_province_year['province'] == 'BC']['cust_count'])
plt.plot(years, churn_per_province_year[churn_per_province_year['province'] == 'QC']['cust_count'])
plt.show()
我不知道如何引用years
部分。
答案 0 :(得分:1)
您想要以下内容吗?
df.groupby(['year', 'province']).apply(churn_per_feature)['cust_count'].unstack().plot(legend=True)
使用matplotlib plt.plot()
:
churn_per_province_year = df.groupby(['year', 'province']).apply(churn_per_feature).reset_index()
#from matplotlib import pyplot
#years = range(2005, 2019) # add the right range here
plt.plot(years, churn_per_province_year[churn_per_province_year['province'] == 'ON']['cust_count'], label='ON')
plt.plot(years, churn_per_province_year[churn_per_province_year['province'] == 'AB']['cust_count'], label='AB')
plt.plot(years, churn_per_province_year[churn_per_province_year['province'] == 'QC']['cust_count'], label='QC')
plt.legend()
plt.show()