Hot answers tagged

3

First filter your results: filtered_df = df[df['investment_in_millions']>10] And then group it by company_sector import numpy as np sectors = filtered_df.groupby(['company_sector']).agg({"investment_in_millions":np.mean}) You can do it in one line: sectors = df[df['investment_in_millions']>10].groupby(['company_sector']).agg({"...


2

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 containing an id and the min/max dates.


2

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


1

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.


1

Alternatively, you can use pd.cut to create your desired bins and then count your observations grouped by the created bins. from faker import Faker from datetime import datetime as dt import pandas as pd # Create sample dataframe fake = Faker() n = 100 start = dt(2020, 1, 1, 7, 0, 0) end = dt(2020, 1, 1, 23, 0, 0) df = pd.DataFrame({"datetime": [...


1

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 to display. Typically, when using a groupby, you need to include all columns that you want to be included in the result, in either the groupby part or the ...


1

df.loc['Total'] = pd.Series([df['Commission'].sum(),df['Profit'].sum(),df['Net profit'].sum()], index = ['Commission','Profit','Net profit'])


1

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 enough R does not have a built in function for that. You could define one yourself using: mode_stat <- function(x) { ux <- na.omit(unique(x)) ux[which.max(tabulate(match(x, ux)))] } and then do in a similar fashion: ...


1

This simple Code worked: Count_sequence = df.groupby(['ID','Sequence']).count() For obtaining the output in an excel sheet: Count_sequence.to_excel('sequence_count.xlsx)


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