-1

I created a dictionary with values such as 2100 and 97AK. I created a dataframe to have values mapped to the dictionary. The values that have mixed numbers and letters are found but all the integers ones are returned as NaN. What could cause the failure of recognizing dictionary values?

abbreviations = {'1700':'Navy', 
'2100': 'Army', 
'5700': 'AF', 
'9700': 'DOD', 
'9736': 'NA', 
}

import pandas as pd
import numpy as np
df = pd.read_excel("fun.xlsx")
columns = ['a', 'bc']
df1 = pd.DataFrame(df, columns=columns)

df1["b"] = df1["ac"].map(abbreviations)
print(df1)
martineau
  • 119,623
  • 25
  • 170
  • 301
newbie
  • 25
  • 4
  • 4
    Do you have any code for us to see? Are the integers being added as strings maybe? – Pep_8_Guardiola Jun 02 '16 at 13:30
  • If all the keys in the dictionary are integers stored as strings, you can convert them to integers with `abbreviations = {int(key): value for key, value in abbreviations.items()}`. – martineau Jun 02 '16 at 14:04

1 Answers1

0

Your abbreviations dictionary shows the integers stored as strings (note the surrounding single quotes in your code). 2100 does not match '2100'.

You either need to populate that dictionary with integers where appropriate, or read/cast your dataframe values to type string. See here: Import pandas dataframe column as string not int

Community
  • 1
  • 1
Pep_8_Guardiola
  • 5,002
  • 1
  • 24
  • 35