pandas to dict: to_dict does not store all values

Mo Re

I have a dataframe df with 40000 rows:

              0  bin
0      4.506840  4-5
1      4.506840  4-5
2      4.444245  4-5
3      4.485975  4-5
4      4.527705  4-5
...         ...  ...
39995  6.572475  6-7
39996  6.697665  6-7
39997  6.322095  6-7
39998  6.322095  6-7
39999  6.676800  6-7

It stores for every number in column '0' the interval (bin) it belongs to. I want to convert it to a dict by:

dict(zip(df[0],df.bin))

to get an output like:

{4.506840: '4-5', 4.506840: '4-5', 4.444245: '4-5, ... }

so I want to store every value from '0' and the bin it belongs to. Somehow my dict has a length of 340, not 40000, so it doesn't store all of the rows. My question is: why? And how do I get all 40000 rows in the dict? Cheers!

Yasser Mohsen

Due to the duplicates you have in your df[0], and due to the fact that you cannot have the same key duplicated in a python dictionary, you can do:

result = {}
for i_0, i_bin in zip(df[0],df.bin):
    if i_0 not in result.keys():
        result[i_0] = []
    result[i_0].append(i_bin)

output:

{
    "4.506840": ["4-5", "4-5"],
    "4.444245": ["4-5"],
    ...
}

It depends on what you want to achieve, but this is a way to perceive all the values.

Edit:

As per @anky comment, you can make use of pandas aggregation function to do the same instead of the loop. Definitely, it is of better performance:

df.groupby(0)['bin'].agg(list).to_dict()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

pandas series to_dict with repeated indices; make dict with list values

Convert Pandas Dataframe to_dict() with unique column values as keys

Pandas to_dict() Returning "Timestamp"

Pandas to_dict modifying numbers

use pandas col to store % of values in dict format

to_dict() creates brackets around values

Pandas to_dict() converts datetime to Timestamp

AttributeError: 'Pandas' object has no attribute 'to_dict'

group by key with pandas series and export to_dict()

psql equivalent of pandas .to_dict('index')

Pandas Dataframe: to_dict() poor performance

Exclude NaNs when using pandas to_dict

Empty dictionary when using Pandas to_dict()

Pandas store dict into json

Pandas: Store DataFrame stats in dict

Appending all values (each containing a list) of a dict to pandas df

Pandas - replace all NaN values in DataFrame with empty python dict objects

Why does pandas.DataFrame.from_dict not support all orient of pandas.DataFrame.to_dict?

How to convert a pandas DataFrame into a dictionary of lists with to_dict()?

pandas to_dict with python native datetime type and not timestamp

Remove index from .to_dict in a described pandas DataFrame

Fasest way to generate dictionaries from a pandas df without to_dict

polars equivalent of pandas set_index() to_dict

Pandas to_dict changes index type with outtype='records'

How to optimize this python pandas code using .to_dict?

How do I remove decimals from Pandas to_dict() output

pandas dataframe to_dict two columns as indexes and third as value

Pandas to_dict data structure, using column as dictionary index

How to use to_dict and orient='records' in Polars that is being used in pandas?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive