复制数据框单元格并将其追加到所有行中数据框末尾的新列

时间:2018-08-01 15:36:34

标签: python pandas csv

我有一个名为'df_main'的数据帧,其中行的数量随着用户每次导入csv而改变。无论行数多大,行“沙龙总数”和列'Paid_BB_Percent'将始终存在。我想从df_main.loc['salon totals', 'Paid_BB_Percent]复制数据,并在数据帧的末尾创建一个新列,称为'Store_BB_Percent',其中该列中的每个单元格都包含此数据的副本。

这个想法是,我将拥有另一个数据框,其中将包含行df_manager = df_main[df_main['Employee'].str.contains(manager_name)],该行将用于通过用户输入查找特定的经理行,然后将添加一个新列,如下所示: df_manager['Manager_BB_Bonus'] = (df_manager['Service_Sales'] - df_manager['Service_Breakpoint']) * (df_manager['Store_BB_Percent']).round(2)

另一种更好的方法是像这样将df_main.iloc['salon totals', 'Paid_BB_Percent']乘以df_manager['Manager_Service_Diff']

df_manager['Manager_BB_Bonus'] = (df_main.loc['salon totals', 'Paid_BB_Percent'] * df_manager['Manager_Service_Diff'])

问题出在我跑步时:

df_manager = df_main[df_main['Employee'].str.contains(manager_name)] df_manager['Service_Breakpoint'] = 1700 df_manager['Manager_Service_Diff'] = (df_manager['Service_Sales'] - df_manager['Service_Breakpoint']) df_manager['Manager_BB_Bonus'] = (df_main.loc['salon totals', 'Paid_BB_Percent'] * df_manager['Manager_Service_Diff']) df_manager

我收到错误消息:“ KeyError:'标签[沙龙总价]不在[索引]中''

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果要乘以三个序列。假设您尝试乘以第一个值

 df_main['salon totals'][0]* df_main['Paid_BB_Percent'][0]* df_main['Manager_Service_Diff'][0]