0

I have a dataframe which has 3 columns. id, date and val. The ids are different. I want to put all of the ids besides eachothers. For example, the first rows consist only one id with different dates and then the second distinct ids and etc. Here is a simple example;

import pandas as pd
df['id'] = [10, 2, 3, 10, 10, 2, 2]
df['date'] = ['2020-01-01 12:00:00','2020-01-01 12:00:00','2020-01-01 12:00:00','2020-01-01 13:00:00','2020-01-01 14:00:00', '2020-01-01 13:00:00','2020-01-01 14:00:00']
df ['val'] = [0, 1, 2,-3, 4, 6,7]

The out put which I want is;

   id          date            val
0   10  2020-01-01 12:00:00     0
1   10  2020-01-01 13:00:00     -3
2   10  2020-01-01 14:00:00     4
3   2   2020-01-01 12:00:00     1
4   2   2020-01-01 13:00:00     6
5   2   2020-01-01 14:00:00     7
6   3   2020-01-01 12:00:00     2

Explanation: We have three ids, 1, 2, 3. At first I want the values based on date for id=1 and then the values based on date for id=2 and etc. The ids does not need to sort.

Can you please help me with that? Thank you

Sadcow
  • 680
  • 5
  • 13
  • so sort by both `id` and `date` – RomanPerekhrest Feb 10 '23 at 21:21
  • If you mean "df.sort_values(by=['date', 'id'],ascending=True)", it does not work. – Sadcow Feb 10 '23 at 21:24
  • Does this answer your question? [How to sort a dataFrame in python pandas by two or more columns?](https://stackoverflow.com/questions/17141558/how-to-sort-a-dataframe-in-python-pandas-by-two-or-more-columns) – G. Anderson Feb 10 '23 at 21:24
  • @G.anderson Thank you. No. It is a little different. – Sadcow Feb 10 '23 at 21:29
  • It's not clear, then, what is wrong with the suggestions so far. Perhaps you could [edit] your question to show what you've tried so far based on your own research, and what is wrong with your current output, and how it's different from your desired output – G. Anderson Feb 10 '23 at 21:33

0 Answers0