为变量赋值并在Pandas中给出KeyError

时间:2015-04-15 19:01:23

标签: python pandas python-2.x

我想总结一个数据帧的列:

print x['count: revenue_sale'].sum()不会出错,并提供正确的输出。

当我尝试y = x['count: revenue_sale'].sum()时,我得到KeyError: 'count: revenue_sale'

def dfCreator(x):

    dcs = [col for col in x.columns if col.startswith("dc")]
    groups = x.groupby(dcs)["uid"].unique()

    groups = list(x.groupby(dcs, as_index=False))
    summed = [i[1].sum() for i in groups]

    x = pd.DataFrame(summed)

    x['interaction_eis_rate'] = x['count: interaction_eis'] / x['dc(uid): interaction_eis']
    x['interaction_eis_reply_rate'] = x['count: interaction_eis_reply'] / x['dc(uid): interaction_eis_reply']
    x['interaction_interaction_match_rate'] = x['count: interaction_match'] / x['dc(uid): interaction_match']
    x['interaction_single_message__rate'] = x['count: interaction_single_message_'] / x['dc(uid): interaction_single_message_']
    x['interaction_single_message_1_rate'] = x['count: interaction_single_message_1'] / x['dc(uid): interaction_single_message_1']
    x['interaction_yes_rate'] = x['count: interaction_yes'] / x['dc(uid): interaction_yes']


    x = x.drop(x.columns[[0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14]], axis=1)

    print(x.columns)

    y = x['count: revenue_sale'].sum()

    return x

pre_purch_m = dfCreator(pre_purch_m) 
pre_purch_f = dfCreator(pre_purch_f)
all_m_rm = dfCreator(all_m_rm) 
all_f_rm = dfCreator(all_f_rm)

print(x.colums)的输出是

Index([u'count: revenue_sale', u'interaction_eis_rate', u'interaction_eis_reply_rate', u'interaction_interaction_match_rate', u'interaction_single_message__rate', u'interaction_single_message_1_rate', u'interaction_yes_rate'], dtype='object')
6045

Index([u'count: revenue_sale', u'interaction_eis_rate', u'interaction_eis_reply_rate', u'interaction_interaction_match_rate', u'interaction_single_message__rate', u'interaction_single_message_1_rate', u'interaction_yes_rate'], dtype='object')
1082

Index([u'dc(uid): interaction_eis', u'interaction_interaction_match_rate', u'interaction_single_message__rate', u'interaction_single_message_1_rate', u'interaction_yes_rate'], dtype='object')

我认为它在第三次函数调用中打破了?

0 个答案:

没有答案