인공지능 AI/머신러닝 Machine Learning

판다스pandas 정렬과 질의query 예제

Tap to restart 2021. 2. 5. 00:00

입력값

import pandas as pd

data_list = []
data = {}
data['name'] = '이서연'
data['gender'] = 'F'
data['age'] = 31
data_list.append(data)
data = {}
data['name'] = '김민준'
data['gender'] = 'M'
data['age'] = 38
data_list.append(data)
data = {}
data['name'] = '박지우'
data['gender'] = 'M'
data['age'] = 45
data_list.append(data)
data = {}
data['name'] = '윤지민'
data['gender'] = 'F'
data['age'] = 23
data_list.append(data)
data = {}
data['name'] = '송유진'
data['gender'] = 'F'
data['age'] = 23
data_list.append(data)

df = pd.DataFrame(data_list)

 

정렬 - 순차

df = df.sort_values(["age"], ascending=[True])

 

정렬 - 역순

df = df.sort_values(["age"], ascending=[False])

 

질의query 예

df = df.query("gender == 'F' and age > 30")

 

pandas - sort query example.ipynb
0.01MB

참고할 자료

10 minutes to pandas