# RayDMatrix reordering dataframe columns

**URL:** <https://discuss.ray.io/t/raydmatrix-reordering-dataframe-columns/3981>\
**Category:** Ray Data\
**Created:** [October 29, 2021, 4:54am UTC](https://discuss.ray.io/t/raydmatrix-reordering-dataframe-columns/3981 "2021-10-29T04:54:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arindam\_Jati](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/arindam_jati/32/1650_2.png) [@Arindam\_Jati](https://discuss.ray.io/u/Arindam_Jati)\
**Post date:** [October 29, 2021, 4:54am UTC](https://discuss.ray.io/t/raydmatrix-reordering-dataframe-columns/3981/1 "2021-10-29T04:54:58Z")

</div>

`RayDMatrix` reorders the columns of a dataframe particularly when `label` argument is provided. Here is a minimal example:

```auto
(Pdb)
df = pd.DataFrame(np.random.randn(5, 4), columns=['1','2','3','10'])

(Pdb) df
          1 2 3 10
0 -0.591416 -0.052763 -1.406966 -1.590726
1 -0.056861 -1.206818 -0.337770 -2.061194
2 0.716264 -0.934042 1.241450 -0.099843
3 -0.346618 0.603396 -1.095848 -0.758888
4 -1.293752 -0.684838 0.206635 -0.549543

(Pdb) dset = RayDMatrix(df, label='3', num_actors=1)

(Pdb) dset.get_data(0)
{'data': 1 10 2
0 -0.591416 -1.590726 -0.052763
1 -0.056861 -2.061194 -1.206818
2 0.716264 -0.099843 -0.934042
3 -0.346618 -0.758888 0.603396
4 -1.293752 -0.549543 -0.684838, 
'label': 0 -1.406966
1 -0.337770
2 1.241450
3 -1.095848
4 0.206635
Name: 3, dtype: float64, 'weight': None, 'base_margin': None, 'label_lower_bound': None, 'label_upper_bound': None

```

The columns of `data` are re-ordered. It should be in the original order `[1, 2, 10]` .  
This is possibly happening because of `x = x[x.columns.difference(exclude_cols)]` [here](https://github.com/ray-project/xgboost_ray/blob/88c3188741650ac7c6e2383315b637c4462f65e7/xgboost_ray/matrix.py#L253).

Can someone please help me understand if I am doing something wrong here?

---

<div class="post-metadata">

**Author:** ![amogkam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/amogkam/32/17_2.png) [@amogkam](https://discuss.ray.io/u/amogkam)\
**Post date:** [November 5, 2021, 6:16am UTC](https://discuss.ray.io/t/raydmatrix-reordering-dataframe-columns/3981/2 "2021-11-05T06:16:09Z")

</div>

Hey @Arindam_Jati thanks for pointing this out!

This should be fixed on master once [Keep ordering of columns on conversion to RayDMatrix by amogkam · Pull Request #170 · ray-project/xgboost\_ray · GitHub](https://github.com/ray-project/xgboost_ray/pull/170) is merged!
