Home > Mobile >  modify a dataframe column with a dictionary
modify a dataframe column with a dictionary

Time:04-06

I have a column in my dataframe that has a value that is an identifier. And in turn I have a dictionary that contains for each identifier its meaning.

I would like to replace the identifier of my column with its meaning.

I thought it was as simple as doing the following (the column with the identifier is in position 3 of the dataframe),

df.iloc[:,3] = my_dict[df.iloc[:,3]]]

But I get the following error,

TypeError: unhashable type: 'Series'.

The column in question contains integers and the dictionary looks like the following:

my_dict = {1: "One", 2: "Two", 3: "Three"}

How could I make this change to my dataframe?

Thank you very much.

CodePudding user response:

I would recommand using a lambda function applied to your column

def explain_column(x,my_dict):
    if x in my_dict.keys():
        return my_dict[x]
    else:
        return x #Assuming that you won't change the value if not in the dict

df['my_column']=df['my_column'].apply(lambda x: explain_column(x,my_dict))

CodePudding user response:

You could use .map()

Example:

import pandas as pd

data = [[1,2,3],
        [2,3,4]]

columns = ['a','b', 'c']

df = pd.DataFrame(data, columns=columns)


my_dict = {1: "One", 2: "Two", 3: "Three"}
df['a'] = df['a'].map(my_dict).fillna(df['a'])

Output:

print(df)
   a  b  c
0  1  2  3
1  2  3  4

Is now (note I only applied to columns 'a' and 'c', and if you don't want nan, then need to use the .fillna():

print(df)
     a  b      c
0  One  2  Three
1  Two  3      4
  • Related