[英]More efficient way to iterate groupby Pandas dataframe?
I have this snippet code that groupby column ID
from a pandas dataframe and appends in a result dataframe all the top salaries from a unique ID.我有这个片段代码,它从熊猫数据框中按列ID
分组,并将来自唯一 ID 的所有最高工资附加到结果数据框中。 The code works but is kind of slow with larger files.该代码有效,但对于较大的文件来说有点慢。 I was wondering if someone could suggest a more efficient way.我想知道是否有人可以提出更有效的方法。
groupe = df.groupby("ID")
t = (group.sort_values(by="Salary", ascending=False)[:1] for yr, group in groupe)
result = pd.DataFrame()
for i in t:
result = result.append(i)
df.groupby('ID').max()
You can then select the salaries column.然后,您可以选择工资列。
Edit编辑
If you want to retain all other columns, even the non-numerical, this should do the job:如果您想保留所有其他列,即使是非数字列,也应该这样做:
df.sort_values(by="Salary", ascending=False).groupby('ID').first()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.