Filling a dataframe from a dictionary keys and values: efficient way

Martino Olivieri

I have the following dataframe as an example.

df_test = pd.DataFrame(data=0, index=["green","yellow","red"], columns=["bear","dog","cat"])

I have the following dictionary with keys and values that are the same or related to the index and columns od my dataframe.

d = {"green":["bear","dog"], "yellow":["bear"], "red":["bear"]}

I filled my dataframe according with the keys and values that are presented, using:

for k, v in d.items():
    for x in v:
        df_test.loc[k, x] = 1

My problem here is that the dataframe and the dictionary I'm working with are very large and it took too much time to compute. Is there a more efficient way to do it? Maybe iterating over rows in the dataframe instead of keys and values in the dictionary?

jezrael

Because performance is important use MultiLabelBinarizer:

d = {"green":["bear","dog"], "yellow":["bear"], "red":["bear"]}

from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
df = pd.DataFrame(mlb.fit_transform(list(d.values())),
                  columns=mlb.classes_,
                  index=list(d.keys()))
print (df)
        bear  dog
green      1    1
yellow     1    0
red        1    0

And then add missing columns and index labels by DataFrame.reindex:

df_test = df.reindex(columns=df_test.columns, index=df_test.index, fill_value=0)
print (df_test)
        bear  dog  cat
green      1    1    0
yellow     1    0    0
red        1    0    0

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Filling a dataframe from a dictionary keys and values

分類Dev

retrieve values from dataframe using keys in dictionary

分類Dev

Most efficient way to get values from a dictionary into a set

分類Dev

Construct a dictionary from a list of dictionary keys, sub-keys and values

分類Dev

Creating a dictionary with values identical to keys from another dictionary

分類Dev

How to convert dictionary to dataframe when values of keys are list of list?

分類Dev

Converting nested dictionary to dataframe with the keys as rownames and the dictionaries in the values as columns?

分類Dev

Update nested dictionary values from dataframe

分類Dev

Swap keys for values in dictionary?

分類Dev

Python: How to generate DataFrame from 2 keys exist in the big dictionary

分類Dev

Create dictionary from/with function arguments as keys and default values as value

分類Dev

keep only keys without None values in dictionary from pandas groups

分類Dev

From Pandas series, create dictionary with unique elements as keys, and their indices as values

分類Dev

add keys and values to a dictionary from multiple objects by for loop

分類Dev

Efficient way of replacing character string with numeric values based on data frame "dictionary"

分類Dev

Converting pandas dataframe into dictionary where keys are index and values are list of column values

分類Dev

Assign list values to dictionary keys

分類Dev

Assigning values to keys in a mutable dictionary

分類Dev

Match keys to values in dictionary in python

分類Dev

Compare keys of dictionary with values of another dictionary

分類Dev

Hadoop Mapper filling with MapOutputBuffer objects, is there a better way to skip bad keys?

分類Dev

Python pandas: convert dictionary to DataFrame with keys as row

分類Dev

Finding an efficient way to reshape a dataframe: from n*m to 1*(n+m)

分類Dev

How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

分類Dev

Generate a string from a dictionary keys (combinations) and assing a boolean value based on values

分類Dev

Efficient way to loop through GroupBy DataFrame

分類Dev

How to add multiple values to dictionary keys in Python

分類Dev

Edit nested dictionary duplicate values in same keys

分類Dev

How to sum up values of duplicate keys in a dictionary?

Related 関連記事

  1. 1

    Filling a dataframe from a dictionary keys and values

  2. 2

    retrieve values from dataframe using keys in dictionary

  3. 3

    Most efficient way to get values from a dictionary into a set

  4. 4

    Construct a dictionary from a list of dictionary keys, sub-keys and values

  5. 5

    Creating a dictionary with values identical to keys from another dictionary

  6. 6

    How to convert dictionary to dataframe when values of keys are list of list?

  7. 7

    Converting nested dictionary to dataframe with the keys as rownames and the dictionaries in the values as columns?

  8. 8

    Update nested dictionary values from dataframe

  9. 9

    Swap keys for values in dictionary?

  10. 10

    Python: How to generate DataFrame from 2 keys exist in the big dictionary

  11. 11

    Create dictionary from/with function arguments as keys and default values as value

  12. 12

    keep only keys without None values in dictionary from pandas groups

  13. 13

    From Pandas series, create dictionary with unique elements as keys, and their indices as values

  14. 14

    add keys and values to a dictionary from multiple objects by for loop

  15. 15

    Efficient way of replacing character string with numeric values based on data frame "dictionary"

  16. 16

    Converting pandas dataframe into dictionary where keys are index and values are list of column values

  17. 17

    Assign list values to dictionary keys

  18. 18

    Assigning values to keys in a mutable dictionary

  19. 19

    Match keys to values in dictionary in python

  20. 20

    Compare keys of dictionary with values of another dictionary

  21. 21

    Hadoop Mapper filling with MapOutputBuffer objects, is there a better way to skip bad keys?

  22. 22

    Python pandas: convert dictionary to DataFrame with keys as row

  23. 23

    Finding an efficient way to reshape a dataframe: from n*m to 1*(n+m)

  24. 24

    How to create three separate lists of values from a list of dictionaries where each dictionary has three keys

  25. 25

    Generate a string from a dictionary keys (combinations) and assing a boolean value based on values

  26. 26

    Efficient way to loop through GroupBy DataFrame

  27. 27

    How to add multiple values to dictionary keys in Python

  28. 28

    Edit nested dictionary duplicate values in same keys

  29. 29

    How to sum up values of duplicate keys in a dictionary?

ホットタグ

アーカイブ