繁体   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