how to go through each column in reverse dataframe in python

You can use pandas iloc indexing to reverse the order of columns in a dataframe.

main.py
Here's an example:

```python
import pandas as pd

df = pd.DataFrame(data={'col1': [1, 2, 3], 'col2': [4, 5, 6], 'col3': [7, 8, 9]})
print(df)

# Output:
#    col1  col2  col3
# 0     1     4     7
# 1     2     5     8
# 2     3     6     9

reversed_df = df.iloc[:, ::-1]
print(reversed_df)

# Output:
#    col3  col2  col1
# 0     7     4     1
# 1     8     5     2
# 2     9     6     3
```

In the `iloc` indexer, the `:` operator indicates all the rows in the dataframe, and the `::-1` slice operator selects all of the columns in reverse order.
553 chars
26 lines

related categories

gistlibby LogSnag