如何使用iterrows在Python中更正数据帧中的值?

2024-04-26 02:42:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个从CSV文件构建的数据框,并将其推送到绘图可视化中。问题是我注意到了几个极端的异常值。我正在处理的数据集中有两个是风速和阵风。基本上,我希望风速大于阵风值的任何值都被重新定义为阵风

我已经研究了ItErrors,我想我很接近了,我得到了一个键值=0,我尝试运行for循环,我读到这是因为我试图循环一个索引数据帧。然而,我看到的每一个修复都没有奏效。如果此条件成立,我如何逐行遍历每个数据帧并将值重新定义到另一个数据帧

以下是我的相关代码片段:

 #Imports of data above this snippet

    #Create tables with monthly mean & max wind speeds
    #Ignore null values
    wind = pd.DataFrame(df, columns = ['wind_speed'])
    wind.dropna(how = 'any', inplace = True)
    wind['wind_speed'] = wind['wind_speed'].astype(str).astype(float)
    wind_m = wind.resample('M').mean()
    wind_max = wind.resample('M').max()

    #Limit to May-November
    wind_m = wind_m[wind_m.index.month.isin([5,6,7,8,9,10])]
    wind_max = wind_max[wind_max.index.month.isin([5,6,7,8,9,10])]

    #Build the same mean and max tables for wind gusts
    gust = pd.DataFrame(df, columns = ['wind_gust'])

    #Drop all rows that don't contain a gust (inplace)
    gust.dropna(how = 'any', inplace = True)
    #Convert data types from objects to datetime and float
    gust['wind_gust'] = gust['wind_gust'].astype(str).astype(float)
    gust_m = gust.resample('M').mean()
    gust_max = gust.resample('M').max()

    #Limit to May-November
    gust_m = gust_m[gust_m.index.month.isin([5,6,7,8,9,10])]
    gust_max = gust_max[gust_max.index.month.isin([5,6,7,8,9,10])]

    #Impose a catch that redefines any wind greater than the gust value as a gust
    i = 0
    for index, row in wind_max.iterrows():
        if wind_max[i] > gust_max[i]:
            gust_max[i] = wind_max[i]    # this is where I return the key error
        i += 1

   #Visualizations below this snippet

**编辑:每个请求的可视化和数据集示例:

enter image description here

请注意红线超过蓝线的异常值,这永远不会发生。如果红线超过蓝色,则应重新定义为阵风(蓝色)

数据集示例:

列:站点ID、日期时间、风速、阵风

enter image description here


Tags: 数据forindex定义meanthismaxresample