# How would I remove a column from a dataframe by distributing its values among existing columns?

**URL:** https://discourse.julialang.org/t/how-would-i-remove-a-column-from-a-dataframe-by-distributing-its-values-among-existing-columns/44265
**Category:** General Usage
**Created:** [August 4, 2020, 3:11pm UTC](https://discourse.julialang.org/t/how-would-i-remove-a-column-from-a-dataframe-by-distributing-its-values-among-existing-columns/44265 "2020-08-04T15:11:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)
#### Post date: [August 4, 2020, 3:11pm UTC](https://discourse.julialang.org/t/how-would-i-remove-a-column-from-a-dataframe-by-distributing-its-values-among-existing-columns/44265/1 "2020-08-04T15:11:26Z")

</div>

Hi how’s it going?

I have a situation where I’m keeping counts of certain visits, and in some scenarios the person visits multiple places during one trip. However, I can’t have this as part of my data, and need it to be redistributed. Let me get to the example so this doesn’t sound like complete jargon!

```julia
df = DataFrame(:person=>["bob","phil","nick"],:london=>[1,1,0],:spain=>[1,0,0],Symbol("london,spain")=>[1,1,1])

```

![Screenshot from 2020-08-04 11-09-10](https://global.discourse-cdn.com/julialang/original/3X/9/b/9b7e298b81bd02df5f0e2fdc3a34c5888eab6637.png)

What I would like to do is scan all the columns that have a delimiter in it, and then redistribute those columns among existing columns in the dataframe (if the existing column exists). Here would be the desired output.

![Screenshot from 2020-08-04 11-10-58](https://global.discourse-cdn.com/julialang/original/3X/e/b/ebc24cf9b33c9844a0d279e44dac073bffb58b51.png)

Thank you!

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [August 4, 2020, 4:44pm UTC](https://discourse.julialang.org/t/how-would-i-remove-a-column-from-a-dataframe-by-distributing-its-values-among-existing-columns/44265/2 "2020-08-04T16:44:21Z")

</div>

This is a tough question! Here is one solution

```julia
julia> df = DataFrame(:person=>["bob","phil","nick"],:london=>[1,1,0],:spain=>[1,0,0],Symbol("london,spain")=>[1,1,1]);

julia> function fix_name(df)
       for name in names(df)
           if occursin(",", name)
               vals = df[!, name]
               inner_names = split(name, ",")
                   for n in inner_names
                      df[!, n] = df[!, n] .+ vals
                   end
               select!(df, Not(name))
               return(df)
           end
       end
        return nothing
       end;

julia> let df = df
       while df != nothing
       df = fix_name(df)
       end
       end

julia> df
3×3 DataFrame
│ Row │ person │ london │ spain │
│ │ String │ Int64 │ Int64 │
├─────┼────────┼────────┼───────┤
│ 1 │ bob │ 2 │ 2 │
│ 2 │ phil │ 2 │ 1 │
│ 3 │ nick │ 1 │ 1 │

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [August 4, 2020, 5:04pm UTC](https://discourse.julialang.org/t/how-would-i-remove-a-column-from-a-dataframe-by-distributing-its-values-among-existing-columns/44265/3 "2020-08-04T17:04:26Z")

</div>

That’s a good solution, and I don’t have time to come up with a full one myself, but this seems to me like a case that might benefit from a wide-to-long transformation as it’s often painful to have relevant info encoded in column names like this. So I might start with:

```julia
julia> stack(df, names(df[:, Not(:person)]))
9×3 DataFrame
│ Row │ person │ variable │ value │
│ │ String │ Categorical… │ Int64 │
├─────┼────────┼──────────────┼───────┤
│ 1 │ bob │ london │ 1 │
│ 2 │ phil │ london │ 1 │
│ 3 │ nick │ london │ 0 │
│ 4 │ bob │ spain │ 1 │
│ 5 │ phil │ spain │ 0 │
│ 6 │ nick │ spain │ 0 │
│ 7 │ bob │ london,spain │ 1 │
│ 8 │ phil │ london,spain │ 1 │
│ 9 │ nick │ london,spain │ 1 │

```

and go from there

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [August 5, 2020, 12:16am UTC](https://discourse.julialang.org/t/how-would-i-remove-a-column-from-a-dataframe-by-distributing-its-values-among-existing-columns/44265/4 "2020-08-05T00:16:24Z")

</div>

```julia
function row_spread(row)
    dict = Dict{String, Int}()
    for (colname, val) in zip(keys(row), values(row))
        if colname == :person
            continue
        end
        for country in split(string(colname), ",")
            dict[country] = get(dict, country, 0) + val
        end
    end
    new_row = hcat(DataFrame(person = row.person), DataFrame(dict))
    new_row
end

new_df = reduce(vcat, row_spread(row) for row in eachrow(df))

```

---

<div class="post-metadata">

### Author: ![Julia1](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Julia1](https://discourse.julialang.org/u/Julia1)
#### Post date: [August 6, 2020, 5:02pm UTC](https://discourse.julialang.org/t/how-would-i-remove-a-column-from-a-dataframe-by-distributing-its-values-among-existing-columns/44265/5 "2020-08-06T17:02:05Z")

</div>

Thanks a lot for the replies guys.
