简体   繁体   中英

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. 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. 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. 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!

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]):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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