data = [['john', 'A01', 1],['john', 'A01', 1],['john', 'A01', 1],['john', 'B01', 0],['john', 'C01', 0],['katty', 'A01', 0],['katty', 'B01', 0]]
df = pd.DataFrame(data, columns = ['name', 'orderID','buying_channel'])
purchase_channel = df.groupby('name').apply(lambda x: pd.Series({"buying_channel": sum((x.buying_channel)/(x.buying_channel.count()))}))
purchase_channel.head()
채널을 구매하는 사람을 계산하고 싶습니다. '1'은 채널, '0'은 다른 채널입니다. (1 = 온라인, 0 = 오프라인)
결과적으로 0.6을 제공하는 전체를 계산하는 대신 orderID를 기반으로 계산하고 싶습니다.
예상 결과:
data2 = [['john', 0.33], ['katty', 0]]
df2 = pd.DataFrame(data2, columns = ['name','buying_channel'])
df2
'존'은 3 번 구매하기 때문에 온라인에서 한 번만 구매하고 나머지는 오프라인에서 구매합니다. 따라서 orderID를 기반으로 집계하는 방법은 무엇입니까?
감사
중복 항목을 삭제 name, orderID
하고 값을 평균화 할 수 있습니다 buying_channel
.
(df.drop_duplicates(['name','orderID'])
.groupby('name', as_index=False)
.buying_channel.mean()
)
산출:
name buying_channel
0 john 0.333333
1 katty 0.000000
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다