Skip to main content
Share Your Experience: Take the 2024 Developer Survey
3 votes
Accepted

How to use df.groupby() to select and sum specific columns w/o pandas trimming total number of columns

I think the answer depends on what you want to do with column 6. Keep in mind that the values for column6 may be different for each groupby on columns 3,4 and 5, so you will need to decide which value ...
Donald S's user avatar
  • 1,959
2 votes

How to impute and aggregate data with ID variant variables for predictive modeling?

This sounds like you're having issues grappling with relational theory. You have focused on the ID column as though it identifies an observed example. But your narrative ("multiple services")...
J_H's user avatar
  • 1,110
2 votes
Accepted

Pandas - Avoid boolean result when using groupby()

First filter your results: filtered_df = df[df['investment_in_millions']>10] And then group it by company_sector ...
Multivac's user avatar
  • 2,979
2 votes

Problem with sort by and group by in pandas

All you need is a groupby operation + aggregation on the min/max values. df.groupby('id').agg(('min','max'))['date_column'] The output should be like this : different dataframe with each line ...
Blenz's user avatar
  • 2,084
2 votes
Accepted

pandas groupby and sort values

If the participant has answered the question 2 before the question 1, you will lose the information on question one by using .agg("first") in the 4th option
mirimo's user avatar
  • 326
1 vote
Accepted

Pandas - Sum of multiple specific columns

df.loc['Total'] = pd.Series([df['Commission'].sum(),df['Profit'].sum(),df['Net profit'].sum()], index = ['Commission','Profit','Net profit'])
SrJ's user avatar
  • 858
1 vote

Calculating Median and Mode of numerical variables for different subgroups in R

assuming your data is stored in an object named df you can do: tapply(df$S_Calls, df$Emp_Stat, median) As for the mode, oddly ...
Iyar Lin's user avatar
  • 799
1 vote

How to group by one column and count frequency from other column for each item in the previous column in python?

This simple Code worked: Count_sequence = df.groupby(['ID','Sequence']).count() For obtaining the output in an excel sheet: ...
Farah's user avatar
  • 21
1 vote

How to combine rows after Pandas Groupby function

I think what you are looking for here is pandas.pivot(). It will transform your table from long to wide format. I first adapted your "Dep Relationship" column so that each child has a unique ...
danielOh's user avatar
1 vote
Accepted

Group rows partially [Python] [Pandas]

You can do this by making use of shift to create different groups based on consecutive values of the states column, after which ...
Oxbowerce's user avatar
  • 7,582
1 vote

How to groupby and sum values of only one column based on value of another column

I figured a way to do it, but it doesn't look efficient at all. Regardless, the following code got me the results I needed. ...
MushyMush's user avatar
1 vote
Accepted

Using Pandas.groupby.agg with multiple columns and functions

You could perhaps generate the dictionary using a list comprehension style syntax. E.g. ...
namiyousef's user avatar
1 vote

Grouping by Multi-Indices of both Row and Column

Move "type" to the vertical axis with pandas.DataFrame.stack df.stack(level=1) Then you can group by "year"
Ivan Reshetnikov's user avatar
1 vote
Accepted

How to find median/average values between data frames with slightly different columns?

Assuming that you have the data stored in separate dataframes, you can use a combination of pandas.concat and ...
Oxbowerce's user avatar
  • 7,582
1 vote

panda grouping dates by variable with transposed value variables

Sorry about that, I thought the link would be enough. So, I have a df containing the German regional states and its country towns and some codes indicating policies. In the columns are the dates as ...
KevG.'s user avatar
  • 11
1 vote
Accepted

Access keys of pandas dataframe when using groupby

You may use df.groupby(['BirthDate', 'ZipCode']).size().reset_index().rename(columns={0: 'n'}) and now you have a data frame that you can easily manipulate.
David Masip's user avatar
  • 6,081
1 vote

Pandas Groupby datetime by multiple hours

Alternatively, you can use pd.cut to create your desired bins and then count your observations grouped by the created bins. ...
sabacherli's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible