根据字典的键创建一个新列?

Creating a new column based on the key of a dictionary?

我正在尝试在使用字符串文字和键的字典项的 for 循环中的数据框中创建一个新列,但它会抛出一条 "ValueError: cannot set a frame with no defined index and a scalar" 错误消息。

exp 类别的字典定义

  d = {'Travel & Entertainment': [1,2,3,4,5,6,7,8,9,10,11], 'Office supplies & Expenses': [13,14,15,16,17],
    'Professional Fees':[19,20,21,22,23], 'Fees & Assessments':[25,26,27], 'IT Expenses':[29],
    'Bad Debt Expense':[31],'Miscellaneous expenses': [33,34,35,36,37],'Marketing Expenses':[40,41,42],
    'Payroll & Related Expenses': [45,46,47,48,49,50,51,52,53,54,55,56], 'Total Utilities':[59,60],
    'Total Equipment Maint, & Rental Expense': [63,64,65,66,67,68],'Total Mill Expense':[70,71,72,73,74,75,76,77],
    'Total Taxes':[80,81],'Total Insurance Expense':[83,84,85],'Incentive Compensation':[88],
    'Strategic Initiative':[89]}

正在基于主数据帧创建新数据帧

mcon = VA.loc[:,['Expense', 'Mgrl', 'Exp Category', 'Parent Category']]
mcon.loc[:,'Variance Type'] = ['Unfavorable' if x < 0 else 'favorable' for x in mcon['Mgrl']]
mcon.loc[:,'Business Unit'] = 'Managerial Consolidation'
mcon = mcon[['Business Unit', 'Exp Category','Parent Category', 'Expense', 'Mgrl', 'Variance Type']]
mcon.rename(columns={'Mgrl':'Variance'}, inplace=True)

正在创建一个新数据帧,最终将写入 excel

a1 = pd.DataFrame() 
for key, value in d.items():
    umconm = mcon.iloc[value].query('Variance < 0').nsmallest(5, 'Variance')
    fmconm = mcon.iloc[value].query('Variance > 0').nlargest(5, 'Variance')
    if umconm.empty == False or fmconm.empty == False:
        a1 = pd.concat([a1,umconm,fmconm], ignore_index = True)
    else:
        continue
a1.to_csv('example.csv', index = False)

输出看起来像这样

我正在尝试添加一个新列,其中显示 Higher/Lower budget than {key},其中 key 代表费用类型,使用以下代码

for key, value in d.items():
    umconm = mcon.iloc[value].query('Variance < 0').nsmallest(5, 'Variance')
    umconm.loc[:,'Explanation'] = f'Lower than budgeted {key}'
    fmconm = mcon.iloc[value].query('Variance > 0').nlargest(5, 'Variance')
    fmconm.loc[:,'Explanation'] = f'Higher than budgeted {key}'
    if umconm.empty == False or fmconm.empty == False:
        a1 = pd.concat([a1,umconm,fmconm], ignore_index = True)
    else:
        continue

但是使用上面的字符串字面量给我错误信息 "ValueError: cannot set a frame with no defined index and a scalar"

如果您能帮助我更正此问题或找到其他解决方案以将此字段添加到我的数据框中,我将不胜感激。提前致谢!

我太傻了,解决方法如下:

for key, value in d.items():
    umconm = mcon.iloc[value].query('Variance < 0').nsmallest(5, 'Variance')
    umconm['Explanation'] = f'Higher than Budget for {key}'
    fmconm = mcon.iloc[value].query('Variance > 0').nlargest(5, 'Variance')
    fmconm['Explanation'] = f'Lower than Budget for {key}'
    if umconm.empty == False or fmconm.empty == False:
        a1 = pd.concat([a1,umconm,fmconm], ignore_index = True)
    else:
        continue

在此数据框中创建新列时,我不必使用 .loc!

出现这个错误是因为这一行

umconm = mcon.iloc[value].query('Variance < 0').nsmallest(5, 'Variance')

有时会产生没有索引的空数据帧。当你想设置你的列(不是 loc)时,而是使用这种方法:

a['Explanation'] = f'Lower than budgeted {key}'