How to put elements of a pandas index as values of a dictionary?

Advertisements I’m struggling with an error in pandas which is driving me crazy. I want to build a dictionary which extracts some data from a pandas df: Index_col col1 P1 F1-R1 P2 F1-R1 P3 F1-R1 P4 F1-R1 P5 F1-R2 P6 F1-R2 P7 F1-R2 P8 F1-R2 (etc) Would give: {‘F1-R1’: [‘P1’, ‘P2’, ‘P3’, ‘P4’], ‘F1-R2’: [‘P5’,… Read More How to put elements of a pandas index as values of a dictionary?

How to further break down a column based on another column after grouping it?

Advertisements I want to find the number of suicides that occurred from year 1985-2016. Which i did easily by df.groupby(‘year’).suicides_no.sum() Now i want to break down the total number of suicides happened per year further into male and female. The column name is ‘sex’ which contains two values – male , female. >Solution : Simply… Read More How to further break down a column based on another column after grouping it?

what is dtype at the last line while checking dtypes for all columns in Python Pandas dataframes

Advertisements What is the dtype at the last line here in the output, i am confused – df.dtypes #A int64 #B bool #C object #dtype: object Here i am only asking about the last line which is ‘dtype: object’. I am not asking why it is not str, my question is what is this line… Read More what is dtype at the last line while checking dtypes for all columns in Python Pandas dataframes

group nearby timestamps together in pandas

Advertisements I understand Pandas has a pd.Grouper where we can specify time frequency. However, it uses the frequency as border for each sample, similar to how resample does it. For example: df.groupby(pd.Grouper(key=’Timestamp’, freq=’1s’)).agg({…}) will create a grouped dataframe with index that are 1s apart. However, I want to group all rows where the difference between… Read More group nearby timestamps together in pandas

convert a dictionary of pandas date frame into one excel file with different sheets

Advertisements I have a dictionary where the keys are names of pandas data frames and the items are the data frames themselves. I want to save this file into an excel file with different sheets, where the name of each sheet is one key of the dictionary and the data inside the sheet is the… Read More convert a dictionary of pandas date frame into one excel file with different sheets

Is pandas groupby() function always produce a DataFrame with the same order respect to the column we group by?

Advertisements Given pd.DataFrame X with column id, is the call to groupby() always return a DataFrame with the same order with respect to the column id? X.groupby(‘id’)[col_name].first() >Solution : No, by default, groupby sorts the rows on the grouping key(s). You can disable this behavior by passing sort=False: X.groupby(‘id’, sort=False)[col_name].first() Example: df = pd.DataFrame({‘id’: [3,… Read More Is pandas groupby() function always produce a DataFrame with the same order respect to the column we group by?