Search code examples
pythonpandasdataframenanseries

nan values when creating a pd.Series through a function


Assume we have the following dataframe

ap_comp = pd.DataFrame({'Name': ['Troll', 'Legolas'],'Code': [111, 222]})

and I passed it through the following function

a_mapping = pd.Series(apcompl['Code'], index=apcompl['Name']).to_dict()

my question is why the a_mapping returns as

{'Troll': nan, 'Legolas': nan}

why the nan appears? Should' t it be the following

{'Troll': 111, 'Legolas': 222}

Solution

  • You have NaNs because of index alignment. The Series you pass in has an index that is different from the values passed as index, internally the constructor performs a reindexing, which gives NaN.

    You would have needed to pass raw values (with .tolist(), .values or .to_numpy()) to the constructor, not a Series:

    a_mapping = pd.Series(ap_comp['Code'].tolist(), index=ap_comp['Name']).to_dict()
    

    But, much better, use:

    a_mapping = ap_comp.set_index('Name')['Code'].to_dict()
    

    output:

    {'Troll': 111, 'Legolas': 222}