python - Pandas 中具有多个 IF 条件的 For 循环

标签 python pandas

我想用 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')

但是当我运行这段代码并与手动计算进行比较时,我发现只有第一个条件计算是正确的,而其他条件则不成立。 (df.one、df.two 和 df. Three 是其他列。)

  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  

最佳答案

您可以使用numpy.select对于获取列名称格式:

for i in range (1980,2015,5):
    m1 = df["DWWC{}".format(i)] <=18.25
    #inverted m1 mask by ~
    m2 = ~m1 & (df["DWWC{}".format(i)] <= 36.5)
    a = (((10-33)/(5))*(df["DWWC{}".format(i)]-5))+10 
    b = ((10/(df.two-df.three))*(df["DWWC{}".format(i)]-df.three))+df.Three
    c = (((df.Three_value-6)/(df.three-5))*(df["DWWC{}".format(i)]-6

    df["MWTP{}".format(i)] = np.select([m1,m2],[a,b], default=c)

关于python - Pandas 中具有多个 IF 条件的 For 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54265041/

相关文章:

python - 如何在这个 pandas 数据框中分组、排序和计算差异?

python - 统计所有数据库中所有表的所有行数

python - 使用 python 中的正则表达式匹配非常长且复杂的版本号

python - 减去时间戳的列给我 TypeError

python - 使用 Pandas value_counts() 添加 'rest' 组

python pandas dataframe 仅为阈值中的数据创建 bin

python - 基于其他列 pandas 相同值的列的数学运算

python - mixin 的顺序如何影响派生类?

python - Django-haystack 通用 SearchView - 无结果

python - 将 SQLAlchemy 查询到 Pandas DF 时重复的列?