{data dendrites} exploring data science with Python et al.

replace( ) for DataFrames

In Python/Pandas we have:

Output:

before: 
|   | icecream | miles    | something | opinion |
|---|----------|----------|-----------|---------|
| 1 | 40920    | 8.326976 | 0.953952  | 3       |
| 2 | 14488    | 7.153469 | 1.673904  | 2       |
| 3 | 26052    | 1.441871 | 0.805124  | 1       |

after:
|   | icecream | miles    | something | opinion |
|---|----------|----------|-----------|---------|
| 1 | 40920    | 8.326976 | 0.953952  | good    |
| 2 | 14488    | 7.153469 | 1.673904  | OK      |
| 3 | 26052    | 1.441871 | 0.805124  | bad     |

Julia's equivalent (I don't know yet if this function is already implemented):


The output is the same as above. (Note: apparently, in 0.4 release, the dictionary syntax will change.)

Unfortunately, if you feed the function an incomplete list, e.g:

replace!(dating_df, :opinion, {1 => "bad", 2 => "OK"})
head(dating_df, 3)

...then you get key not found: 3. I can write a for loop, but I am trying to think of something more elegant.

comments powered by Disqus