我的数据框如下:
name tag price
0 x1 tweak1 1.1
1 x1 tweak2 1.2
2 x1 base 1.0
3 x2 tweak1 2.1
4 x2 tweak2 2.2
5 x2 base 2.0
我想从价格列中减去基本价格并创建一个新列,如下所示:
name tag price sensitivity
0 x1 tweak1 1.1 0.1
1 x1 tweak2 1.2 0.2
2 x1 base 1.0 0.0
3 x2 tweak1 1.3 -0.7
4 x2 tweak2 2.4 0.4
5 x2 base 2.0 0.0
并最终删除带有标记库的行以获取
name tag price sensitivity
0 x1 tweak1 1.1 0.1
1 x1 tweak2 1.2 0.2
3 x2 tweak1 1.3 -0.7
4 x2 tweak2 2.4 0.4
在pandas中执行此操作的最佳方法是什么?
答案 0 :(得分:2)
你可以试试这个:
(df.groupby('name', group_keys=False)
.apply(lambda g: g.assign(sensitivity = g.price - g.price[g.tag == "base"].values))
[lambda x: x.tag != "base"])
或者另一个选项,将数据透视表转换为宽格式,进行减法,然后将其转换回长格式:
wide_df = df.pivot_table(['price'], 'name', 'tag')
(wide_df.sub(wide_df[('price', 'base')], axis=0)
.drop(('price', 'base'), 1).stack(level=1)
.reset_index())
答案 1 :(得分:1)
以下是我将如何处理它:
1)为基础
创建一列2)减去那些列
3)放下底座(没有双关语)
import pandas as pd
import numpy as np
# Creates a column 'Base' If 'Tag' is base and use the value from price
# if 'Tag' is not base, use 0
df['Base'] = np.where(df.tag.isin(['base']), df['Price'] ,0)
# takes the difference of the two columns
df['difference'] = df['Price'] - df['Base']
# Creates a new DF that uses all values except when 'Tag' is base
df3 = df[df['Tag'] !='Base']
print(df3)
以下是我用来提出代码的示例。如果您愿意,请随意关注:
import re
import pandas as pd
import numpy as np
df = pd.DataFrame({'A' : [1,1,3,4,5,5,3,1,5,np.NaN],
'B' : [1,np.NaN,3,5,0,0,np.NaN,9,0,0],
'C' : ['AA1233445','AA1233445', 'rmacy','Idaho Rx','Ab123455','TV192837','RX','Ohio Drugs','RX12345','USA Pharma'],
'D' : [123456,123456,1234567,12345678,12345,12345,12345678,123456789,1234567,np.NaN],
'E' : ['Assign','Unassign','Assign','Ugly','Appreciate','Undo','Assign','Unicycle','Assign','Unicorn',]})
print(df)
df['Base'] = np.where(df.E.isin(['Assign']), df['A'] ,0)
df['difference'] = df['B'] - df['Base']
df3 = df[df['E'] !='Assign']
输出:
A B C D E Base difference
1 1.0 NaN AA1233445 123456.0 Unassign 0.0 NaN
3 4.0 5.0 Idaho Rx 12345678.0 Ugly 0.0 5.0
4 5.0 0.0 Ab123455 12345.0 Appreciate 0.0 0.0
5 5.0 0.0 TV192837 12345.0 Undo 0.0 0.0
7 1.0 9.0 Ohio Drugs 123456789.0 Unicycle 0.0 9.0
9 NaN 0.0 USA Pharma NaN Unicorn 0.0 0.0
答案 2 :(得分:1)
我首先来自'name'
和'tag'
列的索引。
然后我减去'base'
横截面。熊猫将为我们调整。
最后,使用assign
+ drop
+ reset_index
进行记账和格式化。
p = df.set_index(['name', 'tag'])[['price']]
p.assign(sensitivity=p - p.xs('base', level=1)).drop('base', level=1).reset_index()
name tag price sensitivity
0 x1 tweak1 1.1 0.1
1 x1 tweak2 1.2 0.2
2 x2 tweak1 1.3 -0.7
3 x2 tweak2 2.4 0.4