1

I made the following dataframe from a .xlsx (I called it df)

   candle_date  low_price  hight_price  open_price  close_price
0   2020-06-05 189,000.00   192,160.00  189,140.00   190,820.00
1   2020-06-05 189,000.00   190,940.00  189,000.00   189,120.00
2   2020-06-05 189,000.00   191,340.00  191,120.00   189,140.00
3   2020-06-05 190,220.00   191,700.00  190,860.00   191,100.00
4   2020-06-05 189,020.00   191,980.00  189,780.00   190,840.00
..         ...        ...          ...         ...          ...
95  2020-06-01 189,220.00   192,300.00  190,380.00   189,480.00
96  2020-06-01 188,000.00   191,220.00  188,000.00   190,360.00
97  2020-06-01 190,000.00   190,440.00  190,440.00   190,000.00
98  2020-06-01 190,440.00   190,780.00  190,460.00   190,440.00
99  2020-06-01 190,440.00   192,340.00  190,460.00   190,440.00

[100 rows x 5 columns]

plt.plot(df.iloc[:,3], color = "green")

But when graphing it does it from the highest date to the smallest date. How can I graph from lesser date to greater date?

Thanks in advance

Help from Thomas Breydo:

   candle_date  low_price  hight_price  open_price  close_price
99  2020-06-01 190,440.00   190,780.00  190,460.00   190,440.00
86  2020-06-01 193,360.00   199,980.00  193,360.00   197,000.00
87  2020-06-01 191,060.00   195,420.00  191,100.00   193,340.00
88  2020-06-01 190,520.00   194,560.00  192,000.00   191,080.00
89  2020-06-01 188,360.00   193,600.00  192,000.00   192,020.00
..         ...        ...          ...         ...          ...
10  2020-06-05 189,000.00   194,200.00  189,760.00   189,220.00
11  2020-06-05 189,720.00   194,700.00  190,160.00   190,000.00
13  2020-06-05 190,020.00   193,080.00  190,040.00   190,040.00
7   2020-06-05 189,000.00   193,400.00  189,360.00   190,100.00
0   2020-06-05 189,000.00   191,000.00  191,000.00   189,000.00

[100 rows x 5 columns]

Edit: I need the data to be displayed in descending order, starting from row 99 to row 0: [99,98,97,...,4,3,2,1,0] to be able to graph correctly, it is a time graph.

Edit2: I've tried with:

df['candle_date'] =  pd.to_datetime(df['candle_date'])
df = df.sort_values(by=['candle_date'])

   candle_date  low_price  hight_price  open_price  close_price
99  2020-06-01 190,440.00   190,780.00  190,460.00   190,440.00
86  2020-06-01 193,360.00   199,980.00  193,360.00   197,000.00
87  2020-06-01 191,060.00   195,420.00  191,100.00   193,340.00
88  2020-06-01 190,520.00   194,560.00  192,000.00   191,080.00
89  2020-06-01 188,360.00   193,600.00  192,000.00   192,020.00
..         ...        ...          ...         ...          ...
10  2020-06-05 189,000.00   194,200.00  189,760.00   189,220.00
11  2020-06-05 189,720.00   194,700.00  190,160.00   190,000.00
13  2020-06-05 190,020.00   193,080.00  190,040.00   190,040.00
7   2020-06-05 189,000.00   193,400.00  189,360.00   190,100.00
0   2020-06-05 189,000.00   191,000.00  191,000.00   189,020.00

Same problem! :(

Edit3: I've solved it :) I don't know how to close this. Thanks to those who helped me.

Emmy
  • 111
  • 2
  • You probably need to add details to your question so it it clear to viewers what you are trying to achieve and what problems you are having. – wwii Jun 05 '20 at 13:39
  • Possibly related: [Convert Pandas Column to DateTime](https://stackoverflow.com/questions/26763344/convert-pandas-column-to-datetime) .. Welcome to SO. Please take the [tour] and take the time to read [ask] and the other links found on that page. – wwii Jun 05 '20 at 13:41
  • If the column 'candle_date' is a str format, can convert in datetime? – Emmy Jun 05 '20 at 13:46
  • 1
    Yes you you can change a string column to a datetime column - the link I shared in the previous comment is one of many SO Q&A's regarding this. Searching with variations of `python pandas change column from str to date site:stackoverflow.com` should provide more. – wwii Jun 05 '20 at 13:48
  • 1
    I've solved it :) – Emmy Jun 05 '20 at 14:04
  • The [documentation](https://pandas.pydata.org/docs/user_guide/timeseries.html#converting-to-timestamps) is your friend. .. https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior – wwii Jun 05 '20 at 14:05
  • [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – wwii Jun 05 '20 at 14:05

1 Answers1

1

Problem: sort a DataFrame by values in one column.

Solution: sorted_df = candles_dataframe.sort_values(by=['candle_date']). Then plot sorted_df.

Thomas
  • 859
  • 6
  • 16
  • That’s probably because the dataset isn’t perfectly equidistant—which is fine! The spacing in between points let’s you properly understand how much time has passed between measurements. Are you sure you want to ‘fix’ this? – Thomas Jun 05 '20 at 13:29
  • Actually, looking at your updated problem statement, I’m not sure what you mean by equidistant. Could you please clarify? – Thomas Jun 05 '20 at 13:30
  • Yep, good insight! Searching for pandas concert str column to date time should help you out. – Thomas Jun 05 '20 at 13:55