# Sorting Dataframe By Column Index

We begin by generating some random data

```
import pandas as pd
import numpy as np
sample_df = pd.DataFrame( data=np.random.rand(18).reshape(6,3),
columns=['x-val', 'y-val', 'z-val'] )
sample_df
```

x-val | y-val | z-val | |
---|---|---|---|

0 | 0.929960 | 0.686751 | 0.002423 |

1 | 0.959458 | 0.654311 | 0.391251 |

2 | 0.537430 | 0.391395 | 0.305878 |

3 | 0.087757 | 0.195576 | 0.768340 |

4 | 0.837864 | 0.353326 | 0.544702 |

5 | 0.535682 | 0.807780 | 0.379437 |

Implementing the column index sort. For column index, we set axis=1. For row index we set axis=0.

```
sample_df.sort_index(ascending=False, axis=1)
```

z-val | y-val | x-val | |
---|---|---|---|

0 | 0.002423 | 0.686751 | 0.929960 |

1 | 0.391251 | 0.654311 | 0.959458 |

2 | 0.305878 | 0.391395 | 0.537430 |

3 | 0.768340 | 0.195576 | 0.087757 |

4 | 0.544702 | 0.353326 | 0.837864 |

5 | 0.379437 | 0.807780 | 0.535682 |