根据其他行值的差异在同一 pandas 数据框中创建新行值
Creating new row values within same pandas dataframe based on difference of other row values
下面是已有的df
data = np.array([['','Market','Product Code','Week','Sales','Units'],
['Total Customers',123,1,500,400],
['Total Customers',123,2,400,320],
['Major Customer 1',123,1,100,220],
['Major Customer 1',123,2,230,230],
['Major Customer 2',123,1,130,30],
['Major Customer 2',123,2,20,10],
['Total Customers',456,1,500,400],
['Total Customers',456,2,400,320],
['Major Customer 1',456,1,100,220],
['Major Customer 1',456,2,230,230],
['Major Customer 2',456,1,130,30],
['Major Customer 2',456,2,20,10]])
df =pd.DataFrame(data)
我希望根据 'Market' 列中的行值(客户总数)与 'Market' 列中的行值(主要客户 1 + 主要客户)之间的差值创建新行客户 2).我希望将 'Market' 列中的新行值分配为 'Remaining Customers' 并追加到同一个 df 中。
总的来说,我基本上是想计算出市场的剩余销售额和单位 'Gap'
到目前为止,这是我使用 loc 尝试过的方法,但我一直遇到关键错误。有人可以帮忙吗?
df.loc[df['Market'] == 'Remaining Customers'] =
df.loc[df['Market'] == 'Total Customers']-
(df.loc[df['Market'] == 'Major Customer 1']+df.loc[df['Market'] == 'Major Customer 2'])
请查看此笔记本了解更多详情。
https://nbviewer.jupyter.org/github/emican86/48999037/blob/master/48999037.ipynb
.loc 主要是基于标签的。必须对齐数据并设置标签。
下面是已有的df
data = np.array([['','Market','Product Code','Week','Sales','Units'],
['Total Customers',123,1,500,400],
['Total Customers',123,2,400,320],
['Major Customer 1',123,1,100,220],
['Major Customer 1',123,2,230,230],
['Major Customer 2',123,1,130,30],
['Major Customer 2',123,2,20,10],
['Total Customers',456,1,500,400],
['Total Customers',456,2,400,320],
['Major Customer 1',456,1,100,220],
['Major Customer 1',456,2,230,230],
['Major Customer 2',456,1,130,30],
['Major Customer 2',456,2,20,10]])
df =pd.DataFrame(data)
我希望根据 'Market' 列中的行值(客户总数)与 'Market' 列中的行值(主要客户 1 + 主要客户)之间的差值创建新行客户 2).我希望将 'Market' 列中的新行值分配为 'Remaining Customers' 并追加到同一个 df 中。
总的来说,我基本上是想计算出市场的剩余销售额和单位 'Gap'
到目前为止,这是我使用 loc 尝试过的方法,但我一直遇到关键错误。有人可以帮忙吗?
df.loc[df['Market'] == 'Remaining Customers'] =
df.loc[df['Market'] == 'Total Customers']-
(df.loc[df['Market'] == 'Major Customer 1']+df.loc[df['Market'] == 'Major Customer 2'])
请查看此笔记本了解更多详情。 https://nbviewer.jupyter.org/github/emican86/48999037/blob/master/48999037.ipynb
.loc 主要是基于标签的。必须对齐数据并设置标签。