Search code examples
pythondataframejupyter-notebookcolumnsorting

dataframe not sorting by date


I have a dataframe which looks like:

prediction = set(zip(future_forecast_dates[-120:], I, R))

prediction = pd.DataFrame(prediction)

prediction.columns = ['date', 'Infected', 'recovered']

print(prediction.head())

OUTPUT -

enter image description here

I am trying to make a SIR model

and i am trying to sort this dataframe according to date using the following code:
prediction = prediction.sort_values(by=['date'],axis=1)

but then again i get the error:

enter image description here

I am using jupyter notebook


Solution

  • I think you need to change axis=1 to axis=0.

    import pandas as pd
    
    data = pd.DataFrame({"A": [1, 2, 3, 4],
                         "B": [11, 13, 17, 9]})
    
    print(data.head())
       A   B
    0  1  11
    1  2  13
    2  3  17
    3  4   9
    
    
    data = data.sort_values(by=['B'], axis=0)
    print(data.head())
       A   B
    3  4   9
    0  1  11
    1  2  13
    2  3  17