[英]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.我有组,其中第一列中有组名,第二列中有 characteristcs。 If the order in the second is wrong, the third column should be set to NOT OK.如果第二列顺序错误,则第三列应设置为“NOT OK”。 How do I check if the order in each group is correct?如何检查每组中的顺序是否正确?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.