這裏是我會怎樣解決它:
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
你可以包含你曾經使用過的代碼嗎? –
上面的表格是以更大的代碼序列生成的,因此很難包含代碼。實際的表格也大得多,這只是我需要做的操作的表示。 – Bilentor