SORTING AND ORDERING¶
In [41]:
df = pd.DataFrame({'Employee ID':[111, 222, 333, 444],
'Employee Name':['Chanel', 'Steve', 'Mitch', 'Bird'],
'Salary [$/h]':[35, 29, 38, 20],
'Years of Experience':[3, 4 ,9, 1]})
df
Out[41]:
Employee ID | Employee Name | Salary [$/h] | Years of Experience | |
---|---|---|---|---|
0 | 111 | Chanel | 35 | 3 |
1 | 222 | Steve | 29 | 4 |
2 | 333 | Mitch | 38 | 9 |
3 | 444 | Bird | 20 | 1 |
In [42]:
# 경력을 오름차순으로 정렬하세요
In [43]:
my_dict = {"홍길동" : 23, "김나나" : 34, "Mike" : 28, "Alice": 42}
In [44]:
my_dict
Out[44]:
{'홍길동': 23, '김나나': 34, 'Mike': 28, 'Alice': 42}
In [47]:
sorted(my_dict.values())
Out[47]:
[23, 28, 34, 42]
In [53]:
df.sort_values("Years of Experience")
Out[53]:
Employee ID | Employee Name | Salary [$/h] | Years of Experience | |
---|---|---|---|---|
3 | 444 | Bird | 20 | 1 |
0 | 111 | Chanel | 35 | 3 |
1 | 222 | Steve | 29 | 4 |
2 | 333 | Mitch | 38 | 9 |
In [ ]:
# 경력 내림차순 정렬
In [52]:
df.sort_values("Years of Experience", ascending=False)
Out[52]:
Employee ID | Employee Name | Salary [$/h] | Years of Experience | |
---|---|---|---|---|
2 | 333 | Mitch | 38 | 9 |
1 | 222 | Steve | 29 | 4 |
0 | 111 | Chanel | 35 | 3 |
3 | 444 | Bird | 20 | 1 |
In [54]:
# 이름과 경력으로 정렬하세요
# ( 이름으로 정렬하고, 이름이 같으면 경력으로 정렬하라는 뜻 )
In [57]:
df = pd.DataFrame({'Employee ID':[111, 222, 333, 444, 555],
'Employee Name':['Chanel', 'Steve', 'Mitch', 'Bird','Chanel'],
'Salary [$/h]':[35, 29, 38, 20, 35],
'Years of Experience':[3, 4 ,9, 1, 6]})
In [59]:
df
Out[59]:
Employee ID | Employee Name | Salary [$/h] | Years of Experience | |
---|---|---|---|---|
0 | 111 | Chanel | 35 | 3 |
1 | 222 | Steve | 29 | 4 |
2 | 333 | Mitch | 38 | 9 |
3 | 444 | Bird | 20 | 1 |
4 | 555 | Chanel | 35 | 6 |
In [60]:
df.sort_values( ['Employee Name' , 'Years of Experience' ])
Out[60]:
Employee ID | Employee Name | Salary [$/h] | Years of Experience | |
---|---|---|---|---|
3 | 444 | Bird | 20 | 1 |
0 | 111 | Chanel | 35 | 3 |
4 | 555 | Chanel | 35 | 6 |
2 | 333 | Mitch | 38 | 9 |
1 | 222 | Steve | 29 | 4 |
In [61]:
# 이름과 경력으로 정렬하되,
# 이름은 오름차순이고, 경력은 내림차순으로 저열하세요.
In [64]:
df.sort_values( [ 'Employee Name' , ('Years of Experience' ,ascending=False )])
Cell In [64], line 1 df.sort_values( [ 'Employee Name' , ('Years of Experience' ,ascending=False )]) ^ SyntaxError: invalid syntax
In [66]:
df.sort_values( [ 'Employee Name' , 'Years of Experience' ] , ascending= [True,False])
Out[66]:
Employee ID | Employee Name | Salary [$/h] | Years of Experience | |
---|---|---|---|---|
3 | 444 | Bird | 20 | 1 |
4 | 555 | Chanel | 35 | 6 |
0 | 111 | Chanel | 35 | 3 |
2 | 333 | Mitch | 38 | 9 |
1 | 222 | Steve | 29 | 4 |
In [ ]:
# 인덱스를 내림차순으로 정렬하세요
In [67]:
df
Out[67]:
Employee ID | Employee Name | Salary [$/h] | Years of Experience | |
---|---|---|---|---|
0 | 111 | Chanel | 35 | 3 |
1 | 222 | Steve | 29 | 4 |
2 | 333 | Mitch | 38 | 9 |
3 | 444 | Bird | 20 | 1 |
4 | 555 | Chanel | 35 | 6 |
In [70]:
df.sort_index(ascending=False) # ascending 디폴트값은 True
Out[70]:
Employee ID | Employee Name | Salary [$/h] | Years of Experience | |
---|---|---|---|---|
4 | 555 | Chanel | 35 | 6 |
3 | 444 | Bird | 20 | 1 |
2 | 333 | Mitch | 38 | 9 |
1 | 222 | Steve | 29 | 4 |
0 | 111 | Chanel | 35 | 3 |
'DataScience > Pandas' 카테고리의 다른 글
Pandas Tip[1] 문자열 컬럼의 슬라이싱. str (0) | 2022.11.30 |
---|---|
Pandas concat(), merge() 여러 데이터 프레임을 하나로 합치는 방법 (0) | 2022.11.25 |
Pandas 사용자 정의 함수사용 .apply(), 판다스내장.str라이브러리 (0) | 2022.11.25 |
Pandas 카테고리컬, groupby(), 특정 데이터 가져오기 (0) | 2022.11.24 |
Pandas NaN을 처리하는 전략 dropna(), fillna() (0) | 2022.11.24 |