简体   繁体   English

如何遍历和比较数据帧的值?

[英]How to iterate through and compare values of data frames?

I want to iterate through and compare the values in two data frames, PORResult , p90Result and then update the values in a third data frame, CountResult based on that comparison.我想通过迭代和在两个数据帧,该值进行比较PORResultp90Result ,然后更新的第三数据帧中的值, CountResult基于该比较。 If the value in PORResult is greater than the value in p90result I want to update the value in CountResult to be = 1. All of the data frames are the same size (121x365) and have the same column names and indexes.如果值PORResult比的值大p90result我想更新的价值CountResult是= 1。所有的数据帧的大小相同(121x365),并具有相同的列名和索引。 This is where I'm stuck:这是我被困的地方:

for index, row1 in PORResult.iterrows():
    for index, row2 in p90Result.iterrows():
        if PORResult.loc[index,row1]>p90Result.loc[index,row2]:
            CountResult.at[index,row1]=1

When I try this I get the following error message: "None of [Float64Index([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,\\n ...\\n nan, nan, nan, nan, nan, nan, nan, nan, nan, 46.0],\\n dtype='float64', name='day', length=365)] are in the [columns]" I'm not sure where to go from here.当我尝试此操作时,我收到以下错误消息:“[Float64Index([ nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,\\n ...\\n nan, nan, nan , nan, nan, nan, nan, nan, nan, 46.0],\\n dtype='float64', name='day', length=365)] 在 [columns]" 我不知道去哪里从这里。 Does this mean that my data frames are set up incorrectly or am I not executing the loop properly?这是否意味着我的数据框设置不正确,或者我没有正确执行循环? I'm new to python so any help is appreciated!我是 python 的新手,所以感谢任何帮助!

This loop compares all the rows of the first dataframe with all the rows of the second dataframe.此循环将第一个数据帧的所有行与第二个数据帧的所有行进行比较。 You should make loop such as for i in range(PORResult.shape[0]): for j in range (PORResult.shape[1]):您应该进行循环,例如for i in range(PORResult.shape[0]): for j in range (PORResult.shape[1]):

This is how I was able to resolve the problem这就是我解决问题的方法

for day in PORResult.columns:
    for year in PORResult.index:
        tmax = PORResult.loc[year, day]
        perc = p90Result.loc[year, day].values[0]
        if tmax > perc:
            CountResult.loc[year, day] = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM