2
srch_destination hotel_booked count
28 1 4
28 5 1
28 8 2
28 11 9
28 14 17
19 11 3
19 2 5
19 5 8
19 6 10
위에서 데이터 프레임을 포맷했다고합시다. 이는 검색이므로 대상 28을 검색 한 4 명이 호텔 1을 예약했다고 가정 해 보겠습니다. 기본적으로 각 검색 대상에 대한 행과 해당 상위 3 개의 예약이 포함 된 데이터 프레임을 얻고 자합니다.파이썬에서 효율적인 데이터 정렬 및 집계?
srch_destination top_hotels
28 14 11 1
19 6 5 2
는 현재, 내 코드는 'C_ID'는 초기 dataframe 및 '는이'원하는 출력입니다 아래 : 그래서이 dataframe에 대한, 우리는 같이 두 개의 행이있을 것입니다. 나는 R에서 왔고이 정렬과 후속 집계를하는 더 효율적인 방법이 있는지 궁금해하고 있습니다.
import numpy as np
import pandas as pd
a = pd.DataFrame()
for ind in np.unique(c_id.srch_destination):
nlarg = c_id[c_id.srch_destination == ind].sort_values('count', ascending = False).head(3)['hotel_booked']
a = a.append({'srch_destination': ind, 'top_hotels': " ".join(map(str, nlarg))}, ignore_index=True)
a.to_csv('out.csv')
내 R 답을 삭제했습니다 ... 미안 해요. 그 점을 놓쳤습니다. 실수로 당신은 파이썬이 R로 포팅되기를 원합니다. – Gopala