我想使用panda DataFrame中的列上的条件进行for循环:
import numpy as np
import pandas as pd
df=pd.DataFrame(pd.read_csv("data.csv"))
print df
DWWC1980 DWWC1985 DWWC1990
16.7140310 16.35661439 15.89201716
20.9414479 18.00822799 15.73516051
33.95022337 51.87065104 73.76376497
144.7000805 136.1462017 130.9143924
54.9506033 75.03339188 93.22994974
For循环条件语句:
for i in range (1980,2015,5):
if any(df["DWWC"+str(i)] <=18.25) :
df['MWTP'+str(i)]=(((10-33)/(5))*(df["DWWC"+str(i)]-5))+10
elif any((df["DWWC"+str(i)] > 18.25) & (df["DWWC"+str(i)] <= 36.5)) :
df['MWTP'+str(i)]=((10/(df.two-df.three))*(df["DWWC"+str(i)]-df.three))+df.Three
else :
df['MWTP'+str(i)]=(((df.Three_value-6)/(df.three-5))*(df["DWWC"+str(i)]-6
df.to_csv('MWTP1.csv',index='ISO3')
但是当我运行这个代码并与手动计算进行比较时,我发现只有第一个条件的计算是正确的,而其他条件的计算是不正确的。(测向一, 测向二,和测向三是其他列。)
MWTP1980 MWTP1985 MWTP1990
25.87096095 30.72758886 37.04060109
-77.06996017 20.00112954 95.22533503
-290.1012655 -640.6304196 -1068.866556
-1845.172654 -1718.865351 -1641.61201
-1397.638671 -2171.737373 -2873.130596
我相信你的问题在于
if elif else
的用法如下:所以当你的第一个条件被满足时,它不会检查其他条件。 试着把它改成这样:
可以使用^{} 和for get columns names
format
:相关问题 更多 >
编程相关推荐