Dimitrios Truchan
Dimitrios Truchan

Reputation: 152

How do I check the order of values in a group of values with Pandas

I have groups where I have the group name in the first column and the characteristcs in the second. If the order in the second is wrong, the third column should be set to NOT OK. How do I check if the order in each group is correct? enter image description here

Upvotes: 0

Views: 303

Answers (1)

areed145
areed145

Reputation: 11

import pandas as pd

df = dict(num=['37671','37671','37671','37671','37671','37671','37671','37671','45559','45559','45559','45559','45559','45559','45559'],
         char=['No Characteristic','Primary','Secondary','Secondary','Secondary','Tertiary','Tertiary','Tertiary','No Characteristic','Secondary','Tertiary','Secondary','Secondary','Secondary','Tertiary'])
df = pd.DataFrame(df)
df

ordered_list = [pd.np.nan, 'No Characteristic','Primary','Secondary','Tertiary']

def check_in_list(row, ordered_list):
    status = 'NOT OK'
    char_idx = ordered_list.index(row['char'])
    char_prev_idx = ordered_list.index(row['char_prev'])
    if char_idx >= char_prev_idx:
        status = 'OK'
    return status

dfs = []
for num in df['num'].unique():
    df_ = df.loc[df['num'] == num, :]
    df_['char_prev'] = df_['char'].shift(1)
    df_['check'] = df_.apply(lambda row: check_in_list(row, ordered_list), axis=1)
    dfs.append(df_)
dfs = pd.concat(dfs)

dfs

Upvotes: 1

Related Questions