Skip to main content

All Questions

Filter by
Sorted by
Tagged with
1 vote
1 answer
55 views

How to merge dictionaries contained in a Pandas dataframe as a groupby operation

Let us consider a pandas dataframe df containing dictionaries in one of its columns (column mydict): mystring mydict 0 a {'key1': 'value1'} 1 a {'key2': 'value2'...
Sheldon's user avatar
  • 4,633
1 vote
1 answer
53 views

Concatenate the rows of each column being grouped by identify

Welcome Help. I would like to merge multiple rows of each ID of a dataframe into a single cell, Below is input format: I have the following list of columns and Dataframe in pandas respectively dict1 =...
kamel Derouiche's user avatar
0 votes
3 answers
65 views

How to create dictionaries using pandas groupby?

I have a dataframe containing values for several variables for each individual in different patient groups. For example, I have age, sex, weight etc. for multiple individuals in patient groups, e.g., ...
sallicap's user avatar
-1 votes
2 answers
90 views

How to merge rows in dictionary python using pandas

i have a dataframe like that MV id NAME ADDRESS DOC DOCTYPE PHONE 1 100 Mark Home 299 NI {123,456} 2 100 John Work A123 Pass {789,101} 3 100 Club what i want ...
Bahy Mohamed's user avatar
-3 votes
1 answer
48 views

Creating Multiple Dataframes based on starting characters in one column and matching to Dictionary

Let’s say I have a dictionary area_codes = { “New Hampshire”:[‘602’] “Vermont”:[‘803’] } And a data frame that looks like name job title phone number Bob doctor 602-123-4567 Joe pharmacist 803-123-...
datagolfer's user avatar
1 vote
2 answers
262 views

Given a pandas dataframe with three columns how can I groupby one columns and return the sub-dataframe rows as a dict

Input: pd.DataFrame, e.g. input = pd.DataFrame({ 'A': [ 'asset_one', 'asset_one', 'asset_two', ], 'B': [ 'item_one', 'item_two', ...
LucaM's user avatar
  • 77
2 votes
1 answer
107 views

Reorganize nested `dict`

This question is connected to [-> here]. I would like to reorganize the following nested dict please: a = { (0.0, 0.0): {'a': [25, 29, nan]}, (0.0, 2.0): {'a': [25, 29, nan], 'b': [25, 35, 31.0]},...
arkriger's user avatar
  • 244
1 vote
1 answer
293 views

How to get a list of dictionary in a single column of pandas dataframe

Hi I have a dataframe in 1NF form which I wanted to change to different format where I can access those values. This is how my dataframe looks like Code ProvType Alias spec_code 1A12 A Hi 1 1A12 B ...
BrownBatman's user avatar
3 votes
3 answers
1k views

Pandas group by column and convert to key:value pair

I want to convert DataFrame using pandas. I would like to convert it into dictionary format like {'Plant Delivering ID': [Ship-To ID]} there are multiple 'Ship-To ID' for single 'Plant Delivering ID' ...
Namita Tare's user avatar
0 votes
1 answer
59 views

How to solve the value error received while working with this dictionary?

I have a dataframe from which the columns are grouped as: {(a, b, c): [('d', e, f)]} with this command: dct = df.groupby(['a','b','c'])[['d','e','f']].apply( lambda g: list(map(tuple, g....
Mushahid Hussain's user avatar
0 votes
1 answer
122 views

Groupby to convert Pandas DataFrame to list of dictionaries

I have the following DataFrame: print(df) business_id software_id quantity price inventory_level 1234 abc 10 25.5 5 4820 ...
Alessandro Ceccarelli's user avatar
0 votes
3 answers
31 views

Assigning multiple values to the same string during dictionary mapping (pandas)

I have the following code below. I am trying to carry out a mapping where the value 0 in the dataframe column 'caffeine' is replaced by 'no' and any other value aside 0 is replaced by 'yes'. However, ...
Caledonian26's user avatar
0 votes
0 answers
61 views

pandas dataframe: groupby, apply two conditions and go back to initial dataframe

I have a panda dataframe df, representing two groups of points identified by their id, their physical coordinates (x, y), R the distance of each point from the center of the coordinates (x_c,y_c), and ...
fslack's user avatar
  • 179
2 votes
1 answer
3k views

Pandas GroupBy to Aggregate Values into Dictionary with Keys from one column and Values based on another column

I'm trying to create an "aggregated" dictionary/JSON-like object inside a cell of a DataFrame where the keys are the values according to another column (for a particular group). Will ...
quant_fin's user avatar
0 votes
2 answers
104 views

Pandas index is sorting on its own

I have a df sorted by person and time. The index is not duplicated, nor is it continuous from 0. I check the difference in time against a threshold depending on row above person time_bought ...
asd's user avatar
  • 1,309
-1 votes
1 answer
60 views

How to create a nested dictionary from a given dataframe?

I got a df like this one: level profile chest_gold chest_silver chest_bronze 1 a TRUE FALSE TRUE 2 a FALSE ...
ianux22's user avatar
  • 405
1 vote
1 answer
246 views

Create Python graphviz Digraph with Pandas

I am trying to make a diagram tree in graphviz.Digraph, I am using Pandas dataframe. By the below query, I am getting the processid's and their dependents id's in a form of a dictionary dependent = df....
Akshay Lokhande's user avatar
1 vote
1 answer
40 views

Append % symbol to dict numeric values in a dataframe column

I have a dataframe like as shown below key, values_list 1, {'ABC':100} 2, {'DEF':100} 3, {'ASE':95,'ABC':5} 4, {'ABC':55,'ASE':40,'DEF':5} 5, {'DEF':90,'ABC':5,'ASE':2.5,'XYZ':2.5} I would like ...
The Great's user avatar
  • 7,673
0 votes
1 answer
45 views

Changing list to dataframe in dictionary

I am writing a dictionary that has to seperate a dataframe into multiple small dataframes based on a certain item that is repeated in the list calvo_massflows. If the items isn't repeated, it'll make ...
PoorPythonProgrammer's user avatar
1 vote
1 answer
45 views

Aggregate pandas rows and store as dicts or other suitable data types

I have pandas DataFrame that I am trying to aggregate and store the select columns as dicts. import pandas as pd df = pd.DataFrame({ 'id': [1, 1, 2], 'lat': [37....
kms's user avatar
  • 2,014
1 vote
2 answers
147 views

Pandas Dataframe to dictionary of dictionaries of lists

I have this a dataframe like this: col1 col2 col3 US 1 1 US 1 2 US 2 1 NL 1 1 US 2 2 DK 1 1 and I would like to get a dictionary of dictionaries of lists grouped by col1, like that: dict = {US:...
Dimitris Petridis's user avatar
-1 votes
3 answers
82 views

Python: Indicate the elements in groupby

I encountered a problem to indicate the element which is not used as the grouping criteria in groupby . I am expecting the output to be in the format of: { "0": { "...
tw0930's user avatar
  • 61
1 vote
2 answers
108 views

df.to_dict make duplicated index (pandas) as primary key in a nested dict

I have this data frame which I'd like to convert to a dict in python, I have many other categories, but showed just two for simplicity Category Name Description Price ...
VeraCode's user avatar
0 votes
2 answers
110 views

Gather data by year and also by industry

I have this very large Dataframe containing statistics for various firms for years 1950 to 2020. I have been trying to divide the data first by year and then by industry code (4 digits). Both 'year' ...
Marie-Pier St-Vincent's user avatar
1 vote
1 answer
359 views

Pandas DataFrame Groupby two columns and get different relation in same keys insert list

I have this table : Head Relation Tail 0 9 1 0 1 10 1 11 2 9 0 23 3 10 1 61 4 9 0 12 5 10 0 66 ...
Vallinox's user avatar
0 votes
1 answer
93 views

Why when I use groupby on two columns the result is NaN but when I do it on one column it works correctly

I use the following dataframe df = pd.DataFrame({'class': 'a a aa aa b b '.split(), 'item': [5,5,7,7,7,6], 'last_PO_code': ['103','103','103','104','103','104'], ...
Lyès SARDI's user avatar
1 vote
1 answer
731 views

Group scatterplot by colour from dictionary in matplotlib (python)

I have a dictionary of data that I would like to plot. My dictionary is currently structured like so: (# = some number) data = { '15': [{'x': #, 'y': #}, {'x': #, 'y': #}, etc..], '16': [{'x': #, 'y': ...
George Henry's user avatar
1 vote
1 answer
86 views

Filling missing values based on multi-column subgroup

I want to fill missing "Age" values of a DataFrame by a mean of a two-column subgroup. df.groupby(["col_x","col_y"])["Age"].mean() The code above returns the ...
Keperoze's user avatar
-2 votes
2 answers
217 views

Group by multiple columns and get median of dict elements as a new column in pandas

I have a dataframe that looks like below: +-------+----------+-------------+-----------------------------------------------------+ | item | category | subcategory | sales_count ...
charlie_boy's user avatar
0 votes
1 answer
71 views

Group rows of dataframe according to dictionary in pandas and sum corresponding numerators

I have a dataframe of error codes, numerators, and their denominators as well as a dictionary of each possible error code and its overarching error type. I'd like to group the error codes in the ...
drymolasses's user avatar
0 votes
1 answer
63 views

How do we get an optimum key value pair from a list of dictionaries in a dataframe column based on certain rules?

I have the following dataframe: Different 'type' can occur at the same 'time', but the need is to only get the 'type' and 'value' based on the following conditions: priority 1: the type importance ...
K_Raikar's user avatar
  • 126
1 vote
1 answer
46 views

Using Grouped results to create Dictionary

I am trying to summarise some data and then after filtering I would to output a dictionary based on the final results. Please see below what I have being able to figure out thus far and an explanation ...
Diop Chopra's user avatar
1 vote
1 answer
67 views

Output pandas dataframe as a multi level python dictionary to support API JSON request

I have an API that ingests time series data as a JSON request and needs the JSON in the following format: [ { "ts": 1630116000000, "values": { "vwc1": 1....
Mark Morris's user avatar
0 votes
1 answer
55 views

Create a grouped DataFrame from a nested dictionary

What is the efficient way to buid a grouped DataFrame from the nested dictionary. Code snippet # Endcoding Description encoding_dict = {"Age":{"Middle": 0, &...
Girish Kumar Chandora's user avatar
0 votes
0 answers
1k views

Use map or applymap on pandas groupby

I have a Pandas dataframe which I am grouping by one of the columns in the dataframe. Then I have a function which needs to be applied once to each group. The function modifies the group. So what I ...
Panda's user avatar
  • 1
0 votes
1 answer
59 views

Appending groupbys to a nested dictionary

Quick summary of my problem: ghGby is a dictionary of keys each corresponding to a dataframe groupby. I would like to divide each groupby into their own groupby's corresponding to each set of days in ...
Bigboss01's user avatar
  • 618
-1 votes
1 answer
32 views

Split Get from Python dictionary

I have a dictionary that I can use the get method to extract values from but I need to subset these values. For example dict_of_measures = {k: v for k, v in measures.groupby('Measure')} And I am ...
PHIL's user avatar
  • 11
0 votes
1 answer
30 views

Get list of values from column based on dictionary with list of indices

I have the following dataframe: >>> price code 1695 765 0 1700 854 1 1702 975 0 1703 452 0 1704 275 0 ... 2169 754 14 2174 ...
Reut's user avatar
  • 1,592
1 vote
1 answer
91 views

How to efficiently count the number of children for each element in a column?

I have a dataframe df as follows. parent_id name 0 t3_35jfjt t1_cr4y72v 1 t3_35jfjt t1_cr4y7m7 2 t3_35jfjt t1_cr4y7p3 3 t1_cr4y72v t1_cr4y92z 4 t3_35jfjt t1_cr4y986 ... ... ...
Akira's user avatar
  • 2,860
0 votes
2 answers
157 views

How to turn a Pandas Dataframe into a Dictionary of Dataframes by grouping columns

I have a DataFrame that was built from 3D data and takes the form: Index: A, B Columns: 1.a, 1.b, 2.a, 2.b I'm trying to unpack this into a dictionary mapping {A, B} to DataFrames with Index {1,2} and ...
Peter's user avatar
  • 3
1 vote
1 answer
95 views

Groupby multiindex columns using dictionary

On a DataFrame with single levels, group the data on columns using a dictionary: df1 = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=['a','b','c','d','e','f','g','h']) dict_col= {'...
pawn's user avatar
  • 15
0 votes
2 answers
73 views

Converting a pandas dataframe to a nested dictionary with specific key

I am trying to create a nested dictionary from a pandas dataframe with the following format: Name Info Location Alias AA InfoA locationA AliasA BB InfoB locationB AliasB CC InfoC locationC ...
Stoyan Radev's user avatar
2 votes
1 answer
31 views

Conversion of dataframe to required dictionary format

I am trying to convert the below data frame to a dictionary Dataframe: import pandas as pd df = pd.DataFrame({'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6], 'c':[4,3,5,5,5,3], 'd':[3,4,5,5,7,8]}) ...
Divya Bojanpalli's user avatar
1 vote
1 answer
252 views

Merge dataframes inside a dictionary of dataframes

I have a dictionary dict of dataframes such as: { ‘table_1’: name color type Banana Yellow Fruit, ‘another_table_1’:...
amongo's user avatar
  • 61
1 vote
1 answer
554 views

Aggregate multiple column values in pandas GroupBy as a dict

I have a problem related to this question: Aggregate column values in pandas GroupBy as a dict My input data has the following columns: For instance, the input would have the following format language,...
carpediem's user avatar
  • 427
0 votes
1 answer
49 views

saving output from for loop groupby condition

I referred multiple site related to below issue and I tried different ways but did not get the answer. I took sample data and run the groupby with condition, I am getting correct output but when ...
surya ambati's user avatar
0 votes
2 answers
196 views

Clubbing one column values separated by delimiter in Pandas Dataframe based on the groups in another column

I have a dataframe which looks as follows: # df colA colB rqqrs vqtr MNO vqtr japl vqtr nsam2 raqm many ...
Stan's user avatar
  • 884
0 votes
1 answer
392 views

Pandas - How to iterate over groupby to count number of occurences

I have a DF like the below: I want to groupby price, count the number of occurrences where action == N / U / D for each price. ID,Action,indicator,side, price, quantity 7930249,U,0,A,132.938,23 ...
Frittei's user avatar
-1 votes
2 answers
53 views

How to perform group by in python?

I need to read data from an excel file and perform group by on the data after that. the structure of the data is like following: n c 1 2 1 3 1 4 2 3 2 4 2 5 3 1 3 2 3 3 I need to read these ...
Erfan Mohebbi Joo's user avatar
4 votes
1 answer
62 views

Iterate over a groupby dataframe to operate in each row

I have a DataFrame like this: subject trial attended 0 1 1 1 1 1 3 0 2 1 4 1 3 1 7 0 4 1 8 ...
Sebastián Moyano's user avatar