我有兩個數(shù)據(jù)框- OK_df 和 Not_OK_df :OK_df = pd.DataFrame({'type_id' : [1,2,3,3], 'count' : [2,7,2,5], 'unique_id' : ['1|2','2|7','3|2','3|5'], 'status' : ['OK','OK','OK','OK']})Not_OK_df = pd.DataFrame({'type_id' : [1,3,5,6,3,3,3,1], 'count' : [1,1,1,1,3,4,6,3], 'col3' : [1,5,7,3,4,7,2,2], 'unique_id' : ['1|1','3|1','5|1','6|1','3|3','3|4','3|6','1|3'], 'status' : ['Not_OK','Not_OK','Not_OK','Not_OK','Not_OK','Not_OK','Not_OK','Not_OK']})好的_df: type_id count unique_id status0 1 2 1|2 OK1 2 7 2|7 OK2 3 2 3|2 OK3 3 5 3|5 OKNot_OK_df: type_id count col3 unique_id status0 1 1 1 1|1 Not_OK1 3 1 5 3|1 Not_OK2 5 1 7 5|1 Not_OK3 6 1 3 6|1 Not_OK4 3 3 4 3|3 Not_OK5 3 4 7 3|4 Not_OK6 3 6 2 3|6 Not_OK7 1 3 2 1|3 Not_OK在哪里,type_id :對應類型的非唯一 ID。count :從第一次看到 type_id 開始的計數(shù)。unique_id :type_id 和 count 的組合:'type_id|count'col3 :另一列。狀態(tài):有值 - OK 或 Not_OK對于 Ok_df 中的一行,Not_OK_df 中至少有一行具有相同的 type_id,其計數(shù)值小于 OK_df 行的計數(shù)值。我想找到滿足上述條件的 Not_OK_df 行,即Not_OK_df['type_id'] == OK_df['type_id'] & Not_OK_df['count'] < OK_df['count']我嘗試直接使用上述條件,但出現(xiàn)以下錯誤:Reindexing only valid with uniquely valued Index objects我無法將匹配的 type_id 設置為索引來檢索行,因為 type_id 不是唯一的。我不能使用 unique_id 作為索引來檢索,因為它對兩個數(shù)據(jù)幀都是唯一的。預期的輸出是: type_id count col3 unique_id status0 1 1 1 1|1 Not_OK1 3 1 5 3|1 Not_OK2 3 3 4 3|3 Not_OK3 3 4 7 3|4 Not_OK注意:它不包含具有 unique_id 的行:['3|6','1|3'] 因為 OK_df 中沒有具有OK_df['count'] > not_OK_df['count'].如何檢索所需的行。提前致謝。
1 回答

qq_笑_17
TA貢獻1818條經(jīng)驗 獲得超7個贊
如果我對您的理解正確,您的選擇標準如下:
來自的行必須與中的行
Not_ok_df
相同type_id
ok_df
同一行必須具有小于相同行的
count
最大值count
type_id
ok_df
count
首先為每個 unique的最大值創(chuàng)建一個字典type_id
。
max_counts =OK_df.groupby('type_id').max()['count'].to_dict()
然后檢查是否每一行都Not_ok_df
滿足您的條件
Not_OK_df[
Not_OK_df.apply(
lambda not_ok_row: max_counts[not_ok_row['type_id']] > not_ok_row['count'] #returns True if there exists a larger count in ok_df with the same type_id
if not_ok_row['type_id'] in max_counts else False, #checks to see if your Not_ok_df row's type_id exists in ok_df
axis=1
)
]
添加回答
舉報
0/150
提交
取消