Build a Dict of Means from Pandas

pandas to dict
dataframe to dictionary by row
pandas unpack dictionary
if using all scalar values, you must pass an index
dataframe to dictionary with one column as key
python pandas convert dataframe to dictionary with multiple values
pandas dataframe
pandas dataframe from list of dicts

I have a dataframe that looks like this:

    start   stop   duration
0   1       2      1
1   3       4      2
2   2       1      2
3   4       3      0

I'm trying to build a dictionary with key= (start, stop) pairs and the value= avg of their duration, regardless of the order. In other words, (1,2) and (2,1) would both count as an occurrence of the pair (1,2).

Desired output: dict_avg= {(1,2):1.5, (3,4):1}

What's the best way to achieve this?

Here's one possible approach by taking a tuple of the sorted pairs:

>>> grp = df[['start', 'stop']].apply(lambda x: tuple(sorted(x)), axis=1)
>>> df.groupby(grp)['duration'].mean().to_dict()
{(1, 2): 1.5, (3, 4): 1.0}

As a disclaimer, I can almost guarantee this will be significantly slower than the NumPy-sort given here, as using a lambda within .apply() (and needing to use sorted() + tuple() constructor) takes each call in the Python space, rather than letting it be done in Cython/C as you can ideally do via Pandas/NumPy.

pandas.DataFrame.to_dict, DataFrame.mean · pandas.DataFrame. See also. DataFrame.from_dict. Create a DataFrame from a dictionary. df.to_dict('series') {'col1': row1 1 row2 2 Name: col1, dtype: int64, 'col2': row1 0.50 row2 0.75 Name: col2, dtype: float64}. Most pandas users quickly get familiar with ingesting spreadsheets, CSVs and SQL data. However, there are times when you will have data in a basic list or dictionary and want to populate a DataFrame. Pandas offers several options but it may not always be immediately clear on when to use which ones.

Using frozenset not fast but neat , if you looking for efficient way check link

Out[1048]: {(1, 2): 1.5, (3, 4): 1.0}

pandas.DataFrame.from_dict, Construct DataFrame from dict of array-like or dicts. Creates DataFrame object from dictionary by columns or by index allowing dtype specification. Parameters. I have a requirement to create a dictionary within a loop and append them to a pandas data frame with matching key name of dict and column name of data frame. The key value pairs of dictionary in each iteration could be different. An empty pandas data frame df_podcast have been defined at the beginning with all possible keys in the dictionary.

This is also one way:

# sort data based on first two columns

# create the dict of mean

{(1, 2): 1.5, (3, 4): 1.0}

Creating a Pandas Series from Dictionary, mad() to calculate Mean Absolute Deviation of a Series · Python | Pandas Series.​cumsum() to find cumulative sum of a Series · Python | Pandas Series.nonzero()  data dict. Of the form {field : array-like} or {field : dict}. orient {‘columns’, ‘index’}, default ‘columns’ The “orientation” of the data. If the keys of the passed dict should be the columns of the resulting DataFrame, pass ‘columns’ (default). Otherwise if the keys should be rows, pass ‘index’. dtype dtype, default None

from collections import defaultdict

m = defaultdict(list)

for *t, d in zip(*map(df.get, df)):

{k: sum(v) / len(v) for k, v in m.items()}

{(1, 2): 1.5, (3, 4): 1.0}

Python, Given a dictionary of equal length lists, task is to create a Pandas DataFrame from it. There are various Define a dictionary containing ICC rankings. rankings​  pandas.DataFrame.to_dict¶. DataFrame.to_dict(self, orient='dict', into=<class 'dict'>)[source]¶. Convert the DataFrame to a dictionary. The type of the key-value pairs can be customized with the parameters(see below). Parameters. orientstr {‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’} Determines the type of the values of the dictionary.

How to Convert Dictionary to Pandas DataFrame, In this guide, I'll show you the steps to convert a Dictionary to Pandas DataFrame. to create the following tool to convert your dictionary to a DataFrame: 140, window=entry1) def getDataFrame (): my_dict = str(entry1.get())  I can't get the average or mean of a column in pandas. A have a dataframe. Neither of things I tried below gives me the average of the column weight >>> allDF ID birthyear weight 0 619040 1962 0.1231231 1 600161 1963 0.981742 2 25602033 1963 1.3123124 3 624870 1987 0.94212

python pandas dataframe columns convert to dict key and value , From a Python pandas dataframe with multi-columns, I would like to construct a dict from only two count as value in dict. Thank you in  Changed in version 0.23.0: If data is a dict, column order follows insertion-order for Python 3.6 and later.

How to Create DataFrame from Dictionary?, You can create a DataFrame from Dictionary by passing a dictionary as the data argument to DataFrame() class. In this tutorial, find syntax and examples. Varun June 12, 2018 Python Pandas : How to create DataFrame from dictionary ? 2018-08-19T16:57:53+05:30 Pandas, Python 2 Comments In this article we will discuss different techniques to create a DataFrame object from dictionary.