W.R
W.R

Reputation: 187

Update pandas df from dict

I'd like to update the 'frequency' column in my df 'co_names_df_1' from the values in the dict 'counts':

counts:
Counter({u'Apple': 1638, u'Facebook': 1169, u'Amazon': 1027, u'Boeing': 548, u'Microsoft': 437, u'JPMorgan': 435, u'Nasdaq': 364, u'Williams': 296, u'Disney': 270, u'Netflix': 260, u'Chevron': 258, u'Comcast': 213, u'CBS': 200, u'Carnival': 193, u'Intel': 188, u'IBM': 172, u'Starbucks': 165, u'Target': 143, u'Monsanto': 141, u'PayPal': 133, u'Viacom': 126, u'Equifax': 124, u'Anthem': 123, u'Pfizer': 121, u'Nike': 121, u'Caterpillar': 119, u'Citigroup': 116, u'AIG': 116, u'HP': 109, u'Aetna': 109, u'BlackRock': 109 ...

co_names_df_1:
         Name          Frequency
0        3M            0
1        A.O. Smith    0
2        Abbott        0
3        AbbVie        0
4        Accenture     0
5        Activision    0
6        Acuity Brands 0 ...

Upvotes: 1

Views: 1607

Answers (2)

unutbu
unutbu

Reputation: 879511

You could use Series.map:

import collections
import pandas as pd
c = collections.Counter({u'Apple': 1638, u'Facebook': 1169, u'Amazon': 1027, u'Boeing': 548, u'Microsoft': 437, u'JPMorgan': 435, u'Nasdaq': 364, u'Williams': 296, u'Disney': 270, u'Netflix': 260, u'Chevron': 258, u'Comcast': 213, u'CBS': 200, u'Carnival': 193, u'Intel': 188,
                         u'IBM': 172, u'Starbucks': 165, u'Target': 143, u'Monsanto': 141, u'PayPal': 133, u'Viacom': 126, u'Equifax': 124, u'Anthem': 123, u'Pfizer': 121, u'Nike': 121, u'Caterpillar': 119, u'Citigroup': 116, u'AIG': 116, u'HP': 109, u'Aetna': 109, u'BlackRock': 109})
df = pd.DataFrame({'Name': {0: '3M',
                            1: 'A.O. Smith',
                            2: 'Abbott',
                            3: 'AbbVie',
                            4: 'Accenture',
                            5: 'Activision',
                            6: 'Acuity Brands',
                            7: 'AIG'},
                   'Frequency': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 10}})

df['Frequency'] = df['Name'].map(c)
print(df)

yields

            Name  Frequency
0             3M          0
1     A.O. Smith          0
2         Abbott          0
3         AbbVie          0
4      Accenture          0
5     Activision          0
6  Acuity Brands          0
7            AIG        116

I added a row to df to show a non-trivial result.

When there isn't a corresponding key in c, Series.map(c) leaves the Series alone. Thus only rows with a corresponding key in c get updated.

Upvotes: 1

Trevor Witter
Trevor Witter

Reputation: 126

The following iterates through the keys in counts and sets the Frequency value in your dataframe, co_names_df_1, to the value associated with that key in counts.

from collections import Counter

counts = Counter({u'Apple': 1638, u'Facebook': 1169, u'Amazon': 1027, u'Boeing': 548,})

for x in counts:
    co_names_df_1['Frequency'][co_names_df_1['Name']==x] = counts[x]  # updates dataframe values based on those in counts

Update:

Using pandas' .map() method as follows appears to run faster than the above for loop (at least on this small sample set of 4 key:value pairs).

co_names_df_1['Frequency'] = co_names_df_1['Name'].map(counts)

Using %%time in a jupyter notebook cell, the .map() approach takes ~488 µs to run whereas the for loop approach takes ~1.24s

Upvotes: 0

Related Questions