Data Analysis/visualization
[python] 데이터프레임에서 수치형 컬럼 자동 선택 후 그룹별 박스플롯 그리기! (feat. seaborn)
Woomii
2022. 4. 27. 00:08
728x90
반응형
[소스코드]
# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
tmp_df = sns.load_dataset('mpg')
print(tmp_df.head(3))
# Select numerical columns from dataset
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
num_df = tmp_df.select_dtypes(include=numerics)
# fig setting
sns.set(rc = {'figure.figsize':(5, 3)})
sns.set(style="whitegrid")
# numerical col selection & plotting boxplot by group
### ex> 'origin' : usa/japan/europe
for n_col in num_df.columns:
sns.boxplot(y=n_col, x='origin', data=tmp_df)
plt.show()
[실행 결과]
mpg cylinders displacement horsepower weight acceleration \
0 18.0 8 307.0 130.0 3504 12.0
1 15.0 8 350.0 165.0 3693 11.5
2 18.0 8 318.0 150.0 3436 11.0
model_year origin name
0 70 usa chevrolet chevelle malibu
1 70 usa buick skylark 320
2 70 usa plymouth satellite







반응형