根据其他行值的差异在相同的熊猫数据框中创建新的行值(Creating new row values within same pandas dataframe based on difference o

编程入门 行业动态 更新时间:2024-10-25 04:24:42
根据其他行值的差异在相同的熊猫数据框中创建新的行值(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)

我希望根据“市场”列(总客户)中的行值与“市场”列(主要客户1 +主要客户2)中的行值之间的价值差异创建新行。 我希望将“市场”列中的新行值指定为“剩余客户”并附加在同一个DF内。

总体而言,我基本上正在努力解决市场上剩余的销售和单位差距问题

这是我迄今为止使用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'])

Below is an existing 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)

I wish to create new rows based on the value difference between the row value in the 'Market' column (Total Customers) and the row values in the 'Market' Column (Major Customer 1 + Major Customer 2). I wish to assign the new row value in 'Market' Column as 'Remaining Customers' and append within the same df.

Overall, I'm basically trying to work out the remaining Sales and Unit 'Gap' of the market

This is what I have tried so far using loc but I keep getting a key error. Can anyone help?

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主要是基于标签的。 数据必须对齐并设置标签。

Please see this notebook for more details. https://nbviewer.jupyter.org/github/emican86/48999037/blob/master/48999037.ipynb

.loc is primarily label based. Data had to be aligned and labels set.

更多推荐

本文发布于:2023-08-05 03:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:熊猫   框中   差异   数据   Creating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!