0

I have the following dictionary:

{0: 'group',
 1: 'still',
 2: 'earnings',
 3: 'shares',
 4: 'make',
 5: 'finally',
 6: 'amazon',
 7: 'deals',
 8: 'comes',
 9: 'york',
 10: 'iphones'}

print(df)

         0            1            2    ...     53      54         55
0      Group         still       shares  ...    deals  york     iphones
1      amazon        shares       make  ...    finally iphones earnings
...

The latter obtained from stripping an article with:

 df = df["text_articles"].str.split(" ", expand = True) 

How can I translate every row of the dataframe using the dictionary, ultimately creating:

print(final_df)

         0            1            2    ...     53      54         55
0        0            1            3  ...       7       9          10
1        6            3            4  ...       5      10           2
...
Alessandro Ceccarelli
  • 1,775
  • 5
  • 21
  • 41
  • what if some word is not contained in a dictionary? – RomanPerekhrest Nov 24 '18 at 14:14
  • Looks like you want to swap your dictionary keys/values around, and then just `.applymap` that to your DF? – Jon Clements Nov 24 '18 at 14:14
  • I think you can look at [this](https://stackoverflow.com/questions/20250771/remap-values-in-pandas-column-with-a-dict) :) All you have to do is swap you dictionary, as mentioned earlier, and than all the methods at the link can help you :) – Gal Avineri Nov 24 '18 at 14:28

2 Answers2

1

Use:

print (df)
        0       1       2       53       54       55
0   Group   still  shares    deals     york  iphones
1  amazon  shares    make  finally  iphones       aa

First swap keys with values to new dictionary:

d1 = {v:k for k, v in d.items()}

If non exist values is necessary replace to NaNs use map with lower:

df = df.apply(lambda x: x.str.lower().map(d1))
print (df)
   0  1  2  53  54    55
0  0  1  3   7   9  10.0
1  6  3  4   5  10   NaN

Or if want replace non exist value to same scalar, e.g. -1:

df = df.applymap(lambda x: d1.get(x.lower(), -1))
print (df)
   0  1  2  53  54  55
0  0  1  3   7   9  10
1  6  3  4   5  10  -1

And last if want no change:

df = df.apply(lambda x: x.str.lower()).replace(d1)
print (df)
   0  1  2  53  54  55
0  0  1  3   7   9  10
1  6  3  4   5  10  aa
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

I think your request could be captured nicely with df.replace
But first you would have to turn your dictionary around.

d = {v:k for k, v in d.items()}
df = df.replace(d)
Gal Avineri
  • 524
  • 5
  • 13