0

I have the following dataset:

Name Year  Date Value
x    year1 date1 v1
x    year1 date2 v2
x    year1 date3 v3
x    year2 date1 v4
x    year2 date2 v5
x    year2 date3 v6
z    year1 date1 v7
z    year1 date2 v8
z    year1 date3 v9
z    year2 date1 v10
z    year2 date2 v11
z    year2 date3 v12
y    year1 date1 v13
y    year1 date2 v14
y    year1 date3 v15
y    year2 date1 v16
y    year2 date2 v17
y    year2 date3 v18

I would like the following dataset output:

Name Year  Date Value
x    year1 date1 v1
x    year2 date1 v4
x    year1 date2 v2
x    year2 date2 v5
x    year1 date3 v3
x    year2 date3 v6
z    year1 date1 v7
z    year2 date1 v10
z    year1 date2 v8
z    year2 date2 v11
z    year1 date3 v9
z    year2 date3 v12
y    year1 date1 v13
y    year2 date1 v16
y    year1 date2 v14
y    year2 date2 v17
y    year1 date3 v15
y    year2 date3 v18

I tried the following code but my 'Name' column is sorting the x,y,z as well. I want the 'Name' column order to stay as x,z,y: df.sort_values(['Name', 'Date'])

0

2 Answers 2

1

As per my understanding, you want data to be sort first by Name and then by Date.

So, you can try:

df.sort_values(by = ['Name', 'Date'], ascending = [True, True])
Sign up to request clarification or add additional context in comments.

Comments

0

Use:

df.sort_values(['Name', 'Date'])

Output:

    Name    Year    Date    Value
0   x   year1   date1   v1
3   x   year2   date1   v4
1   x   year1   date2   v2
4   x   year2   date2   v5
2   x   year1   date3   v3
5   x   year2   date3   v6
6   y   year1   date1   v7
9   y   year2   date1   v10
7   y   year1   date2   v8
10  y   year2   date2   v11
8   y   year1   date3   v9
11  y   year2   date3   v12

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.