Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 함수
- 이전날짜제거
- SQL
- Def
- vlookup
- trim
- 태블로퍼블릭
- Tableau
- 태블로
- 살려줘
- 정제
- Delete
- 외부접속허용
- TabPy
- random.uniform
- 데이터전처리
- concat
- 맵차트
- putty
- 오류
- D2E8DA72
- 아나콘다
- mysql
- 전처리
- 북마크
- split
- 파이썬
- Join
- safe mode 해제
- 에러
Archives
- Today
- Total
무던히 하다보면 느는
[python] 집계 기본 함수 정리 본문
01. 티켓 클래스에 따른 생존율 비교
train_df[["Pclass", "Survived"]].groupby(["Pclass"], as_index=False).mean() \
.sort_values(by="Survived", ascending=False)
transactions.groupby("month", as_index=False)["amount"].sum() # 그룹별 합계 다른 방식으로
02. 월별 결제 총액 및 시각화
transactions.csv 를 활용하여 2023년 월별 총 결제 금액을 바 차트로 시각화하세요.
transactions = pd.reac_csv("./2504 코딩테스트준비/transactions_csv.csv")
transactions["transaction_date"] = pd.to_datetime(transactions["transaction_date"])
transactions["month"] = transactions["transaction_date"].dt.to_period("M")
transactions
monthly_total = transactions.groupby("month", as_index=False)["amount"].sum()
monthly_total
시각화
더보기


import matplotlib.pyplot as plt
plt.rcParams['font.family'] ='Malgun Gothic'
plt.rcParams['axes.unicode_minus'] =False
plt.figure(figsize=(10, 5))
monthly_total.plot(kind="bar", title="2023년 월별 결제 총액", ylabel="총 금액 (원)")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

02-1.species 별 sepal_length 평균 그래프
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
iris2 = iris.groupby('species', as_index=False)[['sepal_length', 'sepal_width', 'petal_length','petal_width']].mean()
plt.figure(figsize=(20,5))
iris2['sepal_length'].plot.bar(x=iris2.index, color='green', alpha=0.5)
plt.title('Average Sepal Length by Species') # , fontsize=15
plt.xlabel('species')
plt.ylabel('average sepal length')
plt.xticks(fontsize=12, rotation=0)
plt.show()

03. 재구매율 상위 사용자 찾기
한 번 이상 거래한 사용자를 기준으로 2회 이상 결제한 사용자 비율을 계산하세용
user_counts = transactions["user_id"].value_counts()
repurchase_rate = (user_counts_df["count"] >=2.sum()) / len(user_counts)
- .value_counts()
(user_counts_df["count"] >= 2.sum())
user_counts_df = transactions.groupby("user_id", as_index=False).size()
user_counts_df.columns = ["user_id","count"]
user_counts_df = user_counts_df.sort_values(by="count", ascending = False).head(5)
user_counts_df
04. 2023년 가입자 중 1회 이상 결제한 사용자 비율
user_info['joined_date'] = pd.to_datetime(user_info['joined_date'])
joined_2023 = user_info[user_info['joined_date'].dt.year == 2023]
active_users = transactions['user_id'].unique()
active_joined_2023 = joined_2023[joined_2023['user_id'].isin(active_users)]
active_joined_2023
len(active_joined_2023) /len(joined_2023)
- pd.to_datetime(user_info['joined_date']) 데이터 타입 변형 잊지 말기
'SQL > 코딩테스트' 카테고리의 다른 글
[mysql] Investigating a Drop in User Engagement (0) | 2025.04.24 |
---|---|
[mysql] Validating A/B Test Result (0) | 2025.04.24 |
[python] leetcode 집계함수 문제 (1) | 2025.04.18 |
[python] 집계 기본 함수 정리 (2) (0) | 2025.04.17 |
[python] 통계 관련 함수 정리 (0) | 2025.04.17 |