Convert array of objects to string

I am having a MongoDB aggregate result: { "healthDetails": [ { "problem": "Fever", "note": "normal" }, { "problem": "Allergy", "note": "critical" } ] } I need to convert the above array like this: { "healthDetails": "Fever – normal, Allergy – critical" } Need to remove square brackets and double quotes and concat the values by… Read More Convert array of objects to string

In Pandas, how to retrieve the rows which created each group, after aggregation and filtering?

Let import pandas as pd df = pd.DataFrame( { ‘a’: [‘A’, ‘A’, ‘B’, ‘B’, ‘B’, ‘C’], ‘b’: [True, True, True, False, False, True] } ) print(df) groups = df.groupby(‘a’) # "A", "B", "C" agg_groups = groups.agg({‘b’:lambda x: all(x)}) # "A": True, "B": False, "C": True agg_df = agg_groups.reset_index() filtered_df = agg_df[agg_df["b"]] # "A": True, "C":… Read More In Pandas, how to retrieve the rows which created each group, after aggregation and filtering?

How to create 2 new column in DataFrame based on the highest values in rest of column with appropriate prefix in Python Pandas?

I have Pandas DataFrame like below (I can add that my DataFrame is definitely bigger, so I need to do below aggregation only for selected columns): ID | COUNT_COL_A | COUNT_COL_B | SUM_COL_A | SUM_COL_B —–|————-|————-|———–|———— 111 | 10 | 10 | 320 | 120 222 | 15 | 80 | 500 | 500 333… Read More How to create 2 new column in DataFrame based on the highest values in rest of column with appropriate prefix in Python Pandas?

Is there pandas aggregate function that combines features of 'any' and 'unique'?

I have a large dataset with similar data: >>> df = pd.DataFrame( … {‘A’: [‘one’, ‘two’, ‘two’, ‘one’, ‘one’, ‘three’], … ‘B’: [‘a’, ‘b’, ‘c’, ‘a’, ‘a’, np.nan]}) >>> df A B 0 one a 1 two b 2 two c 3 one a 4 one a 5 three NaN There are two aggregation functions… Read More Is there pandas aggregate function that combines features of 'any' and 'unique'?

Group by and aggregate the values in pandas dataframe

I have following dataframe in python meddra_id meddra_label soc cross_ref soc_term 2 10000081 Abdominal pain 10017947 http://snomed.info/id/21522001 Gastrointestinal disorders 3 10017999 Gastrointestinal pain 10017947 http://snomed.info/id/21522001 Gastrointestinal disorders 15 10000340 Abstains from alcohol 10041244 http://snomed.info/id/105542008 Social circumstances 35 10001022 Acute psychosis 10037175 http://snomed.info/id/69322001 Psychiatric disorders 36 10061920 Psychotic disorder 10037175 http://snomed.info/id/69322001 Psychiatric disorders I would like… Read More Group by and aggregate the values in pandas dataframe

Aggregate daily data into weeks

I have data resembling the following structure, where the when variable denotes the day of measurement: ## Generate data. set.seed(1986) n <- 1000 y <- rnorm(n) when <- as.POSIXct(strftime(seq(as.POSIXct("2021-11-01 23:00:00 UTC", tryFormats = "%Y-%m-%d"), as.POSIXct("2022-11-01 23:00:00 UTC", tryFormats = "%Y-%m-%d"), length.out = n), format = "%Y-%m-%d")) dta <- data.frame(y, when) head(dta) #> y when #>… Read More Aggregate daily data into weeks