我有以下数据:
var cartSchema = mongoose.Schema({
orderArray: Array,
date: { type: Date, default: Date.now },
totalActual: Number,
totalSale: Number,
totalSave: Number,
})
我需要检查同名名称下的值是否遵循约束(例如,只有三种类型并且同名下不存在所有类型):s1 如何在Python中完成?感谢您的帮助。data = {
"index": [1, 2, 3, 4, 5],
"name": ["A", "A", "B", "B", "B"],
"type": ['s1', 's2', 's1', 's2', 's3'],
'value': [20, 10, 18, 32, 25]
}
df = pd.DataFrame(data)
答案 0 :(得分:1)
尝试:
#Use pd.Categorical to ensure sorting if column is not lexicographical ordered.
df['type'] = pd.Categorical(df['type'], ordered=True, categories=['s1','s2','s3'])
df['result'] = df.sort_values('type').groupby('name')['value'].diff(-1)
df['result'] = df['result'].lt(0).mask(df['result'].isna(),'')
df
输出:
index name type value result
0 1 A s1 20 False
1 2 A s2 10
2 3 B s1 18 True
3 4 B s2 32 False
4 5 B s3 25