我一直在关注其他stackoverflow问题(例如Python Pandas: Add column based on other column),以开发代码来创建新列,其值就是“ a”列与“ b”列之间的差。
我知道语法相当简单,但是在使用iloc指定要用于计算的列时遇到了问题:
CREATE TRIGGER ModDate_location_update
ON dim_Location
AFTER UPDATE
AS
BEGIN
UPDATE dbo.dim_Location
SET UPDATE_DATE = GETDATE()
WHERE LOCATION_ID IN (SELECT LOCATION_ID FROM INSERTED)
end
我得到了错误:
df['diff'] = df.iloc[:, 0] - df.iloc[:, -1]
答案 0 :(得分:2)
正如Náthali所说,只要您使用数字值,下面的方法就会起作用
SELECT StartTime, AVG(ColumnA/NULLIF(ColumnB,0))
FROM Table
where StartTime between '02-SEP-19 12:00:00.000000000 AM'
and '02-SEP-19 11:59:59.000000000 PM'
group StartTime
ORDER BY StartTime
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(5,2),
columns=["a", "b"])
#178 µs±1.34 µs df.iloc[:, 0] - df.iloc[:, -1]
#84.1 µs±1.02 µs df["a"] - df["b"]
#5.39 µs±84.2 ns df["a"].values - df["b"].values
#702 µs±32.4 µs 您不能减去字符串。
答案 1 :(得分:1)
我正在使用熊猫0.23.4,它可以正常工作
A = pd.DataFrame({'A': [4, 3, 4, 3], 'B': [3,2,6,4]})
A['diff'] = A.iloc[:,0] - A.iloc[:, 1]
In [11]: A
Out[11]:
A B diff
0 4.0 3.0 1.0
1 3.0 2.0 1.0
2 4.0 6.0 -2.0
3 3.0 4.0 -1.0
new NaN NaN NaN