Reputation: 2005
I want to replace the values of one key of a dictionary with a column of dataframe. While the values in the second key are repeated if any of the values from first key are repeated.
For example:
using DataFrames
d = d = Dict(:1 => 2, :2=>3, :3=>4)
df = DataFrame(a = [1,1,2,2,3,3],
b = [7,6,5,4,3,2],
c = [9,8,7,6,6,8])
#Output
Dict{Int64,Int64} with 4 entries:
3 => 4
2 => 7
1 => 6
#Dataframe
8 rows × 3 columns
a b c
Int64 Int64 Int64
1 7 9
1 6 8
2 5 7
2 4 6
3 3 6
3 2 8
I want to replace the keys of Dict = d
with df.a
, to produce the following dictionary.
Dict{Int64,Int64} with 6 entries:
1 => 6
1 => 6
2 => 7
2 => 7
3 => 4
3 => 4
May I know how to achieve this operation?
Firstly, thanks @gTcV for the amazing suggestion and great help, highly appreciate it !!!
Just to extract the values one can follow the above post mentioned by @gTcV. However, to achieve the above dataframe operation one may use the following snippet:
using DataFrames
d = d = Dict(:1 => 2, :2=>3, :3=>4)
df = DataFrame(a = [1,1,2,2,3,3],
b = [7,6,5,4,3,2],
c = [9,8,7,6,6,8])
df[!, c] = [i=d[i] for i in df[:, :a]]
Output: -->
6×3 DataFrame
Row │ a b c
│ Int64 Int64 Int64
─────┼─────────────────────
1 │ 1 7 2
2 │ 1 6 2
3 │ 2 5 3
4 │ 2 4 3
5 │ 3 3 4
6 │ 3 2 4
Thanks!!
Upvotes: 2
Views: 1241
Reputation: 2504
Dicts
do not allow repeated keys, so what you ask for is not possible. If it's just about the output, you can do the following:
for a in df[:,1]
println("$a => $(d[a])")
end
Upvotes: 1