簡體   English   中英

Pandas 如何從一列創建重復列表,並且只保留對應列的最大值?

[英]Pandas How do I create a list of duplicates from one column, and only keep the highest value for the corresponding columns?

我想在第一列Primary Mod Site中找到所有重復項,並且只保留數據集中所有化合物(列 BM)的最高值。 excel片材

對於代碼,我有:

#read desired excel file
df = pd.read_excel("20220825_CISLIB01_Plate-1_Rows-A-B")

#function to find the duplicates in the dataset, sections them, and remove them
#can be applied to any dataset with the same format as original excel files

def getDuplicate():
    gene = df["Primary Mod Site"]
    #creates a list of all of the duplicates in Primary Mod Site
    pd.concat(g for _, g in df.groupby("gene") if len(g) > 1)

我堅持下一步該做什么。 非常感謝幫助!

如果您將數據作為代碼或文本發布以允許復制,這將有所幫助。

但是,IIUC,您需要按“A”列分組,然后從列的 rest 中取最大值,這似乎可以解決問題

df["Primary Mod Site"].max()

根據我在屏幕截圖中注意到的內容(例如前 3 行),具有最高值的行往往在所有列中具有最高值,所以,這樣的事情可能會起作用。

 df = df.sort_values("ONCV-1-1-1", ascending = False).drop_duplicates("Primary Mod Site", keep='first', ignore_index=True)

或者如果不確定該觀察是否對所有行都正確。

可能這會起作用

df = df.groupby("Primary Mod Site").max()

注意:請發布一個可重現的示例,便於復制粘貼供我們測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM