Skip to content
Advertisement

Tag: group-by

Pandas dataframe get first row of each group

I have a pandas DataFrame like following: I want to group this by [“id”,”value”] and get the first row of each group: Expected outcome: I tried following, which only gives the first row of the DataFrame. Any help regarding this is appreciated. Answer If you need id as column: To get n first records, you can use head():

Pandas ‘count(distinct)’ equivalent

I am using Pandas as a database substitute as I have multiple databases (Oracle, SQL Server, etc.), and I am unable to make a sequence of commands to a SQL equivalent. I have a table loaded in a DataFrame with some columns: In SQL, to count the amount of different clients per year would be: And the result would be How

Advertisement