How to increment Python Pandas DataFrame based on key/values from a dictionary

user3661230

Having a list of dictionaries, e.g.:

[{'item_id':'string1','feature1': 1, 'feature2': 0, 'feature3':2},
 {'item_id':'string2','feature1': 0, 'feature2': 1, 'feature3':0},
 {'item_id':'string3','feature1': 2, 'feature2': 0, 'feature3':1},
 {'item_id':'string1','feature1': 1, 'feature2': 0, 'feature3':2}]

I'd like to construct and update a DataFrame in which one of the columns captures an item_id, while the rest should be initiated and their values incrementally updated in case a repetition of an item_id (here 'string1') is detected.

The following:

import pandas as pd

list_of_dictionaries = [{'item_id':'string1','feature1': 1, 'feature2': 0, 'feature3':2},
     {'item_id':'string2','feature1': 0, 'feature2': 1, 'feature3':0},
     {'item_id':'string3','feature1': 2, 'feature2': 0, 'feature3':1},
     {'item_id':'string1','feature1': 1, 'feature2': 0, 'feature3':2}]


header = ['item_id','feature1','feature2','feature3']
df = pd.DataFrame(columns=header)

for d in list_of_dictionaries:
    df = pd.DataFrame.from_dict([d])

obviously only initializes the DataFrame.

Ideally, I'd like to sum up all the feature values for an 'item_id' that have more than 1 occurrence. For the example input 'list_of_dictionaries' this would be:

   item_id  feature1  feature2  feature3
0  string1         2         0         4
1  string2         0         1         0
2  string3         2         0         1
Sait

You can use DataFrame.groupby():

In [47]: df = pd.DataFrame.from_dict(list_of_dictionaries)

In [48]: df.groupby('item_id').sum()
Out[48]:
         feature1  feature2  feature3
item_id
string1         2         0         4
string2         0         1         0
string3         2         0         1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create dictionary of dictionaries from a dataframe using python's pandas

From Dev

How to create a dataframe from dictionary with auto-increment index

From Dev

How to create a dataframe from dictionary with auto-increment index

From Dev

Python: Pandas: making a dictionary from dataframe

From Dev

Dictionary from Pandas dataframe

From Dev

Python + Pandas + Spark - How to import a dataframe into Pandas dataframe and convert it into a dictionary?

From Dev

Filtering a Pythons Pandas DataFrame based on values from dictionary

From Dev

Filtering a Pythons Pandas DataFrame based on values from dictionary

From Dev

Creating Dictionary from Pandas DataFrame Column Based on Unique Values in Column

From Dev

Python Dictionary to Pandas Dataframe

From Dev

How to filter pandas dataframe on multiple columns based on a dictionary?

From Dev

New pandas dataframe column using values from python dictionary

From Dev

Creating a pandas dataframe from a dictionary

From Dev

Pandas dataframe from nested dictionary

From Dev

Pandas map to DataFrame from dictionary

From Dev

Pandas DataFrame from Dictionary with Lists

From Dev

Pandas map to DataFrame from dictionary

From Dev

Find and Add Missing Column Values Based on Index Increment Python Pandas Dataframe

From Dev

How to increment date range by a value from a date record in pandas python?

From Dev

How to increment date range by a value from a date record in pandas python?

From Dev

A dictionary in a Pandas dataframe column in Python

From Dev

Converting a pandas dataframe into python dictionary

From Java

How to create expanded pandas dataframe from a nested dictionary?

From Dev

How to construct pandas DataFrame from dictionary with key as column and row index

From Dev

Python Pandas: How to split a sorted dictionary in a column of a dataframe

From Dev

Python Pandas: How to split a sorted dictionary in a column of a dataframe

From Dev

How to append a dictionary to a pandas dataframe?

From Dev

How to create new rows from values inside in a cloumn of pandas dataframe based on delimeter in Python?

From Dev

Python: how to get values from a dictionary from pandas series

Related Related

  1. 1

    How to create dictionary of dictionaries from a dataframe using python's pandas

  2. 2

    How to create a dataframe from dictionary with auto-increment index

  3. 3

    How to create a dataframe from dictionary with auto-increment index

  4. 4

    Python: Pandas: making a dictionary from dataframe

  5. 5

    Dictionary from Pandas dataframe

  6. 6

    Python + Pandas + Spark - How to import a dataframe into Pandas dataframe and convert it into a dictionary?

  7. 7

    Filtering a Pythons Pandas DataFrame based on values from dictionary

  8. 8

    Filtering a Pythons Pandas DataFrame based on values from dictionary

  9. 9

    Creating Dictionary from Pandas DataFrame Column Based on Unique Values in Column

  10. 10

    Python Dictionary to Pandas Dataframe

  11. 11

    How to filter pandas dataframe on multiple columns based on a dictionary?

  12. 12

    New pandas dataframe column using values from python dictionary

  13. 13

    Creating a pandas dataframe from a dictionary

  14. 14

    Pandas dataframe from nested dictionary

  15. 15

    Pandas map to DataFrame from dictionary

  16. 16

    Pandas DataFrame from Dictionary with Lists

  17. 17

    Pandas map to DataFrame from dictionary

  18. 18

    Find and Add Missing Column Values Based on Index Increment Python Pandas Dataframe

  19. 19

    How to increment date range by a value from a date record in pandas python?

  20. 20

    How to increment date range by a value from a date record in pandas python?

  21. 21

    A dictionary in a Pandas dataframe column in Python

  22. 22

    Converting a pandas dataframe into python dictionary

  23. 23

    How to create expanded pandas dataframe from a nested dictionary?

  24. 24

    How to construct pandas DataFrame from dictionary with key as column and row index

  25. 25

    Python Pandas: How to split a sorted dictionary in a column of a dataframe

  26. 26

    Python Pandas: How to split a sorted dictionary in a column of a dataframe

  27. 27

    How to append a dictionary to a pandas dataframe?

  28. 28

    How to create new rows from values inside in a cloumn of pandas dataframe based on delimeter in Python?

  29. 29

    Python: how to get values from a dictionary from pandas series

HotTag

Archive