How to change the index of columns of a dataframe?

Viewed 61

dataframe as follows:

julia> df
8×6 DataFrame
│ Row │ RegionID │ RegionName   │ StateName    │ SizeRank │ 2008-03 │ 2008-04 │
│     │ Any      │ Any          │ Any          │ Any      │ Any     │ Any     │
├─────┼──────────┼──────────────┼──────────────┼──────────┼─────────┼─────────┤
│ 1   │ 6181     │ New York     │ New York     │ 1        │ missing │ missing │
│ 2   │ 12447    │ Los Angeles  │ California   │ 2        │ 1446    │ 1705    │
│ 3   │ 39051    │ Houston      │ Texas        │ 3        │ 2926    │ 3121    │
│ 4   │ 17426    │ Chicago      │ Illinois     │ 4        │ 2910    │ 3022    │
│ 5   │ 6915     │ San Antonio  │ Texas        │ 5        │ 1479    │ 1529    │
│ 6   │ 13271    │ Philadelphia │ Pennsylvania │ 6        │ 1609    │ 1795    │
│ 7   │ 40326    │ Phoenix      │ Arizona      │ 7        │ 1310    │ 1519    │
│ 8   │ 18959    │ Las Vegas    │ Nevada       │ 8        │ 1618    │ 1856    │

stacked dataframe as follows:

julia> df4=stack(df3,Between(:"2008-03", :"2008-04"),variable_name=:year, value_name=:sales_count)
16×6 DataFrame
│ Row │ year    │ sales_count │ RegionID │ RegionName   │ StateName    │ SizeRank │
│     │ Symbol  │ Any         │ Any      │ Any          │ Any          │ Any      │
├─────┼─────────┼─────────────┼──────────┼──────────────┼──────────────┼──────────┤
│ 1   │ 2008-03 │ missing     │ 6181     │ New York     │ New York     │ 1        │
│ 2   │ 2008-03 │ 1446        │ 12447    │ Los Angeles  │ California   │ 2        │
│ 3   │ 2008-03 │ 2926        │ 39051    │ Houston      │ Texas        │ 3        │
│ 4   │ 2008-03 │ 2910        │ 17426    │ Chicago      │ Illinois     │ 4        │
│ 5   │ 2008-03 │ 1479        │ 6915     │ San Antonio  │ Texas        │ 5        │
│ 6   │ 2008-03 │ 1609        │ 13271    │ Philadelphia │ Pennsylvania │ 6        │
│ 7   │ 2008-03 │ 1310        │ 40326    │ Phoenix      │ Arizona      │ 7        │
│ 8   │ 2008-03 │ 1618        │ 18959    │ Las Vegas    │ Nevada       │ 8        │
│ 9   │ 2008-04 │ missing     │ 6181     │ New York     │ New York     │ 1        │
│ 10  │ 2008-04 │ 1705        │ 12447    │ Los Angeles  │ California   │ 2        │
│ 11  │ 2008-04 │ 3121        │ 39051    │ Houston      │ Texas        │ 3        │
│ 12  │ 2008-04 │ 3022        │ 17426    │ Chicago      │ Illinois     │ 4        │
│ 13  │ 2008-04 │ 1529        │ 6915     │ San Antonio  │ Texas        │ 5        │
│ 14  │ 2008-04 │ 1795        │ 13271    │ Philadelphia │ Pennsylvania │ 6        │
│ 15  │ 2008-04 │ 1519        │ 40326    │ Phoenix      │ Arizona      │ 7        │
│ 16  │ 2008-04 │ 1856        │ 18959    │ Las Vegas    │ Nevada       │ 8        │

Please guide me in displaying columns year and sales_count at the end.

1 Answers

Do this:

select(df4, Not([:year, :sales_count]), :year, :sales_count)

or if you want it shorter (but it relies on the column number of what you want to move):

select(df4, Not(1:2), 1:2)

finally you could also have used indexing e.g. like this:

df4[:, [3:end; 1:2]]

Here I am using positional indexing, but you could also have used column names:

df4[:, Cols(Not([:year, :sales_count]), :year, :sales_count)]
Related