파이썬(Python)
판다스 / pandas 3 - 정렬, 컬럼제거, 데이터 가공, 파생변수 생성
무지성개발자
2021. 8. 6. 00:51
- 데이터 준비 -
pandas 정렬 => .sort_values
.sort_values(by = '컬럼명', ascending = False or True)
customer.sort_values(by = 'is_deleted', ascending = False)
# ascending : 오름차순
pandas 컬럼 제거 => drop
.drop
customer1 = customer.drop(['end_date'], axis = 1)
customer1
데이터 가공 => apply
- 데이터 준비 -
df = pd.DataFrame([
[1,2,3,4],
[7,6,9,2]
])
df
def cal(col):
return col.max()- col.min()
df.apply(cal)
# col에는 컬럼별로 배열이 들어옵니다.
cal(np.array([1,2,6,9,11,15]))
# 15-1 = 14
df[0].apply(lambda x : x*2)
파생변수 생성
customer['point'] = 100
customer
np.zeros((4192, ), dtype=np.int32) + 101
customer['point'] = np.zeros((4192, ), dtype=np.int32) + 101
customer