Right way to reverse pandas.DataFrame?

后端 未结 6 1013
旧巷少年郎
旧巷少年郎 2020-11-27 11:24

Here is my code:

import pandas as pd

data = pd.DataFrame({\'Odd\':[1,3,5,6,7,9], \'Even\':[0,2,4,6,8,10]})

for i in reversed(data):
    print(data[\'Odd\']         


        
6条回答
  •  佛祖请我去吃肉
    2020-11-27 12:02

    None of the existing answers resets the index after reversing the dataframe.

    For this, do the following:

     data[::-1].reset_index()
    

    Here's a utility function that also removes the old index column, as per @Tim's comment:

    def reset_my_index(df):
      res = df[::-1].reset_index(drop=True)
      return(res)
    

    Simply pass your dataframe into the function

提交回复
热议问题