NaN column after using map and replace attribute

Question:

I have a dictionary:

label_to_text:{0: 'emotion1', 4: 'emotion2', 10: 'emotion3', 12: 'emotion4', 14: 'emotion5', 23: 'emotion6'

and a df:

text_df:

index    emotion    
  0         0
  1         10
  2         23
  3         12
  4         4
  5         14

What I try:

I used map to add a new column with the dict.values():

text_df['text'] = text_df['emotion'].map(label_to_text)

But I got this:

text_df:

index    emotion     text
  0         0        NaN
  1         10       NaN
  2         23       NaN
  3         12       NaN
  4         4        NaN
  5         14       NaN

What I expected:

text_df:

index    emotion     text
  0         0        emotion1
  1         10       emotion3
  2         23       emotion6
  3         12       emotion4
  4         4        emotion2
  5         14       emotion5

I also tried with replace instead map, but I got this:

index    emotion     text
  0         0        0
  1         10       10
  2         23       23
  3         12       12
  4         4        4
  5         14       14

This is an image of my dictionary:

dict

when I used value = label_to_text.get(3) I got the value, so the key is correct I think.

Edit: Dict information added.
dict print:
dict print

print(text_df[’emotion’].dtype):
text_df[’emotion’].type

Asked By: Daniel_DS

||

Answers:

You can update each row base on information from another column with df.at like this:

for i in range(len(text_df)):
  text_df.at[i, 'text'] = label_to_text[int(text_df.at[i, 'emotion'])]
Answered By: Jordy

You’re getting NaN because the keys of your dictionnary are numeric while the values of your column are strings. So, you need to strip extra whitespaces, cast to (int) and then you can proceed the map :

text_df["text"] = text_df["emotion"].str.strip().astype(int).map(label_to_text)

Output :

print(text_df)

  index emotion      text
0     0       0  emotion1
1     1      10  emotion3
2     2      23  emotion6
3     3      12  emotion4
4     4       4  emotion2
5     5      14  emotion5
Answered By: Timeless
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.