Python 添加一个循环遍历字段的函数
Python add a function that loops through field
在 Excel 中,我使用了如下计算:
0.16667*10^((C15-121.1)/10)。计算放在 D14 上。
The dataframe
在图片中,我想对索引 0 上的列“0”执行以下计算:
=0.16667*10^((91.03-121.1)/10).这意味着索引 1 上的临时值用于计算索引 0 上的列“0”。整个列必须像这样
进一步说明:
Temp Calculation
0 90.01 0.16667*10^((91.03-121.1)/10)
1 91.03 0.16667*10^((95.06-121.1)/10)
2 95.06 0.16667*10^((100.07-121.1)/10)
3 100.07 0.16667*10^((103.00-121.1)/10)
4 103.00
所以看DF中的计算。计算中的温度比当前索引高一个温度
编辑:
List1= [90.01, 91.03, 95.06, 100.07, 103.50]
test = pd.DataFrame(List1, columns=["Temp"])
那我要加上计算:
test["calculation"] = 0.16667*10^((test["Temp"][1:]-121.1)/10)
这会产生以下错误:
TypeError: Cannot perform 'rxor' with a dtyped [float64] array and scalar of type [bool]
编辑 2:
test["calculation"] = 0.16667*10**((test["Temp"][1:]-121.1)/10).reset_index()
test returns:
Temp calculation
0 90.01 1.6667
1 91.03 16.6670
2 95.06 166.6700
3 100.07 1666.7000
4 103.50 NaN
计算的方式很好。但结果不同。当温度为 100.07 时,结果必须为 0.001314。见下文:
test2 = 0.16667*10**((100.07-121.1)/10)
out: 0.0013147931580348442
您可以引用数据框中的整列并根据需要对它们执行任何计算。
df["answer"] = 0.16667*10**((df["Temp"]-121.1)/10)
编辑:
我明白你想用下一行来计算当前行;
您可以将 pandas 系列视为列表并将它们编入索引,即 [1:]
df["calculation"] = 0.16667*10**((df["Temp"][1:]-121.1)/10).reset_index()
这会将计算列中的最后一个位置保留为 NaN,但如果您以后愿意,可以手动更改它。
EDIT2:疏忽,^在python中是异或,**相当于幂。
.reset_index() 是必需的,否则偏移量将位于数据帧的开头,而不是数据帧的结尾
编辑 3:
使用代码
test["calculation"] = 0.16667*10**((test["Temp"][1:]-121.1)/10).reset_index(drop=True)
我得到结果
Temp calculation
0 90.01 0.000164
1 91.03 0.000415
2 95.06 0.001315
3 100.07 0.002896
4 103.50 NaN
在 Excel 中,我使用了如下计算: 0.16667*10^((C15-121.1)/10)。计算放在 D14 上。
The dataframe
在图片中,我想对索引 0 上的列“0”执行以下计算: =0.16667*10^((91.03-121.1)/10).这意味着索引 1 上的临时值用于计算索引 0 上的列“0”。整个列必须像这样
进一步说明:
Temp Calculation
0 90.01 0.16667*10^((91.03-121.1)/10)
1 91.03 0.16667*10^((95.06-121.1)/10)
2 95.06 0.16667*10^((100.07-121.1)/10)
3 100.07 0.16667*10^((103.00-121.1)/10)
4 103.00
所以看DF中的计算。计算中的温度比当前索引高一个温度
编辑:
List1= [90.01, 91.03, 95.06, 100.07, 103.50]
test = pd.DataFrame(List1, columns=["Temp"])
那我要加上计算:
test["calculation"] = 0.16667*10^((test["Temp"][1:]-121.1)/10)
这会产生以下错误:
TypeError: Cannot perform 'rxor' with a dtyped [float64] array and scalar of type [bool]
编辑 2:
test["calculation"] = 0.16667*10**((test["Temp"][1:]-121.1)/10).reset_index()
test returns:
Temp calculation
0 90.01 1.6667
1 91.03 16.6670
2 95.06 166.6700
3 100.07 1666.7000
4 103.50 NaN
计算的方式很好。但结果不同。当温度为 100.07 时,结果必须为 0.001314。见下文:
test2 = 0.16667*10**((100.07-121.1)/10)
out: 0.0013147931580348442
您可以引用数据框中的整列并根据需要对它们执行任何计算。
df["answer"] = 0.16667*10**((df["Temp"]-121.1)/10)
编辑:
我明白你想用下一行来计算当前行; 您可以将 pandas 系列视为列表并将它们编入索引,即 [1:]
df["calculation"] = 0.16667*10**((df["Temp"][1:]-121.1)/10).reset_index()
这会将计算列中的最后一个位置保留为 NaN,但如果您以后愿意,可以手动更改它。
EDIT2:疏忽,^在python中是异或,**相当于幂。 .reset_index() 是必需的,否则偏移量将位于数据帧的开头,而不是数据帧的结尾
编辑 3:
使用代码
test["calculation"] = 0.16667*10**((test["Temp"][1:]-121.1)/10).reset_index(drop=True)
我得到结果
Temp calculation
0 90.01 0.000164
1 91.03 0.000415
2 95.06 0.001315
3 100.07 0.002896
4 103.50 NaN