简体   繁体   中英

Index based conditional dataframe creation

        Val1       Val2
x 10    1.05       2.01   
x 14    2.98       5.98           
x 16    1.01       1.02  
y 10    0.02       0.07
y 11    0.01       0.01
z 10    2.11       1.17
z 12    0.08       0.08
z 13    3.18       7.10
z 17    2.61       1.78
...
..
.
   

I am trying to add a True / False column with a condition like:

Find the base row(should be 10 in index) like x 10 and find if x 11 's val 1 and val 2 is >= then x 10 's val 1 and val 2 . Find if z 17 's val 1 and val 2 is >= z 10 's val 1 and val 2 so the desired df is like below:

        Val1       Val2      Result
x 10    1.05       2.01      False
x 14    2.98       5.98      True     
x 16    1.07       1.02      False
y 10    0.02       0.07      False
y 11    0.01       0.01      False
z 10    2.11       1.17      False
z 12    0.08       0.08      False
z 13    3.18       1.17      True
z 17    2.61       1.78      True
...
..
.
   

Base rows' results should always be false

Well I made a start like below:

df["Result"] = np.repeat(False, len(df))
for i in range(0, len(df)):
    if df[index][i].str.contains("10") == True:
        base = df[index][i][0]
        for base in df[index]:
            if base[i+1]["val1"] > base[i]["val1"] and base[i+1]["val2"] > base[i]["val2"]:
                df["Result"][i] = True
            else:
                df["Result"][i] = False

But couldn't make it work, what can be the problem?

You can do groupby with transform after create the key by cumsum

g=df.groupby(df.index.str.contains('10').cumsum())
s1=g.Val1.transform('first')
s2=g.Val2.transform('first')
df['new']=s1.lt(df.Val1) & s2.lt(df.Val2)
df
Out[119]: 
      Val1  Val2    new
x 10  1.05  2.01  False
x 14  2.98  5.98   True
x 16  1.01  1.02  False
y 10  0.02  0.07  False
y 11  0.01  0.01  False
z 10  2.11  1.17  False
z 12  0.08  0.08  False
z 13  3.18  7.10   True
z 17  2.61  1.78   True

You can find base values with .groupby().first() and according to this answer you can .join() that to the original df:

df = pd.DataFrame({'Val1': [1.05, 2.98, 1.01, 0.02, 0.01, 2.11, 0.08, 3.18, 2.61], 'Val2': [2.01, 5.98, 1.02, 0.07, 0.01, 1.17, 0.08, 7.10, 1.78]}, index=pd.MultiIndex.from_arrays(arrays=[['x', 'x', 'x', 'y', 'y', 'z', 'z', 'z', 'z'], [10, 14, 16, 10, 11, 10, 12, 13, 17]], names=['letters', 'numbers']))

df = df.join(df.groupby(level=0).first(), rsuffix='_base')
df['Result'] = (df.Val1 >= df.Val1_base) & (df.Val2 >= df.Val2_base)
df.loc[df.index.get_level_values('numbers')==10, 'Result'] = False

Output:

>>> df
                 Val1  Val2  Val1_base  Val2_base  Result
letters numbers
x       10       1.05  2.01       1.05       2.01   False
        14       2.98  5.98       1.05       2.01    True
        16       1.01  1.02       1.05       2.01   False
y       10       0.02  0.07       0.02       0.07   False
        11       0.01  0.01       0.02       0.07   False
z       10       2.11  1.17       2.11       1.17   False
        12       0.08  0.08       2.11       1.17   False
        13       3.18  7.10       2.11       1.17    True
        17       2.61  1.78       2.11       1.17    True

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