0

According to this thread, we could use map or replace to remap values of a Dataframe using a defined dictionary. I have tried this and it did correctly remap the values, but the output result only produces the column I performed the operation on (of type series) instead of the full Dataframe.

How can I perform the mapping but keep the other columns (with 'last') in the new data3 ?

data3 = data['last'].map(my_dict)

1 Answers1

0

I think what you are trying to do is this:

data['last'] = data['last'].map(my_dict)

Updating based on comment with relation to the link:

In [1]: di = {1: "A", 2: "B"}

In [5]: from numpy import NaN

In [6]: df = DataFrame({'col1':['w', 1, 2], 'col2': ['a', 2, NaN]})

In [7]: df
Out[7]:
  col1 col2
0    w    a
1    1    2
2    2  NaN

In [8]: df['col1'].map(di)
Out[8]:
0    NaN
1      A
2      B
Name: col1, dtype: object

In [9]: df
Out[9]:
  col1 col2
0    w    a
1    1    2
2    2  NaN

In [10]: df['col1'] = df['col1'].map(di)

In [11]: df
Out[11]:
  col1 col2
0  NaN    a
1    A    2
2    B  NaN

If you want this to happen in data3 instead of data then you could assign the Series result of the map to a column in data3.

Swagga Ting
  • 602
  • 3
  • 17
  • Thank you for your answer. I don't think this syntax is correct as it produced an error. Besides, I am trying to keep the original data and keep using data3. Kind of what we saw in the link I provided. –  Sep 15 '19 at 13:39
  • hmmm.. it works for me. Maybe I don't understand the question. I updated the answer to the best of my understanding but maybe this is not what you're looking for. Maybe you can be more specific and include the DataFrames? – Swagga Ting Sep 15 '19 at 13:53
  • Yeah it works. But, in this thread I linked it kind works just by calling map without explicitly having to define which column to use on data nor with need to do any assigning. I think it works only in older versions. I guess I'll duplicate the variables and follow your approach. –  Sep 15 '19 at 14:07