Одномерный анализ
Анализируйте каждый столбец независимо: стройте распределения для числовых данных и подсчитывайте значения для категориальных, отмечая выбросы и асимметрию.
«Одномерный анализ» — бесплатный урок Pandas & NumPy Academy на CoddyKit. Это урок 2 из 4. Ты можешь прочитать весь урок бесплатно ниже — а потом практиковать его прямо в браузере с встроенным редактором кода и ИИ-репетитором 24/7. Это часть пути обучения Pandas & NumPy Academy, и твой прогресс синхронизируется между веб-версией и приложением CoddyKit. Курс Pandas & NumPy Academy содержит 4 уроков всего.
Части этого урока еще не переведены и отображаются на английском.
What Is Univariate Analysis?
Univariate analysis examines one column at a time in isolation, ignoring relationships between columns. The goal is to understand each variable's distribution, detect outliers, identify skewness, and spot data quality issues before any modelling begins. Univariate analysis differs for numeric and categorical columns — numerics need distribution plots and summary statistics, while categoricals need frequency counts and proportions.
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic')
numeric_cols = df.select_dtypes('number').columns.tolist()
categoric_cols = df.select_dtypes('object').columns.tolist()
print('Numeric columns:', numeric_cols)
print('Categorical columns:', categoric_cols)Histograms for Numeric Columns
Plot a histogram for each numeric column to see the shape of its distribution. Look for symmetry vs. skewness (tail on one side), modality (one peak vs. several), and obvious anomalies (values at extreme ends that seem implausible). In Pandas, df.hist() creates a quick multi-column histogram grid without writing a loop, but Seaborn's histplot gives finer control for individual columns.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset('titanic')
# Quick multi-column histogram grid
numeric = df.select_dtypes('number')
numeric.hist(bins=20, figsize=(12, 8), layout=(2, 3), color='steelblue', edgecolor='white')
plt.suptitle('Univariate Distributions (Numeric Columns)', y=1.02)
plt.tight_layout()
plt.show()Summary Statistics for Numeric Columns
For each numeric column, compute and compare mean vs. median. When mean > median significantly, the distribution is right-skewed (long right tail, common for income and price data). When mean < median, it is left-skewed. Also check standard deviation relative to the mean: a std larger than the mean for a non-negative variable is a sign of high variability or outliers. Compute skewness directly with df.skew().
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic')
numeric = df.select_dtypes('number')
summary = pd.DataFrame({
'mean': numeric.mean(),
'median': numeric.median(),
'std': numeric.std(),
'skewness': numeric.skew(),
'missing_pct': (numeric.isna().sum() / len(df) * 100).round(1)
}).round(2)
print(summary)Box Plots for Outlier Detection
Box plots are the quickest visual tool for spotting outliers in numeric columns. Any point beyond the whiskers (1.5×IQR from Q1 or Q3) is plotted individually and flagged as a potential outlier. A column with many outlier points warrants investigation: are they data entry errors, legitimate extreme values, or evidence of a non-normal distribution? Box plots also reveal right or left skew from the asymmetric position of the median inside the box.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset('titanic')
fig, axes = plt.subplots(1, 3, figsize=(14, 5))
for ax, col in zip(axes, ['age', 'fare', 'sibsp']):
df[[col]].boxplot(ax=ax)
ax.set_title(f'Box Plot: {col}')
plt.tight_layout()
plt.show()IQR-Based Outlier Counts
The IQR (Interquartile Range) method defines outliers as values below Q1 - 1.5×IQR or above Q3 + 1.5×IQR. You can compute this programmatically to count and inspect outliers in every numeric column without plotting. This is useful in automated pipelines where you need to log anomaly counts rather than generate charts. Deciding what to do with outliers — remove, cap, or flag — should be a deliberate choice informed by domain knowledge.
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic')
numeric = df.select_dtypes('number')
Q1 = numeric.quantile(0.25)
Q3 = numeric.quantile(0.75)
IQR = Q3 - Q1
lower = Q1 - 1.5 * IQR
upper = Q3 + 1.5 * IQR
outlier_counts = ((numeric < lower) | (numeric > upper)).sum()
print('Outlier counts per column:')
print(outlier_counts[outlier_counts > 0])Value Counts for Categorical Columns
For each categorical column, call value_counts() to see how observations are distributed across categories. Always check: does any single category dominate (>80%)? This causes class imbalance in classification tasks. Are there rare categories with only 1-2 observations (typos or data entry errors)? Does the distribution match business expectations (e.g. a 'country' column that shows most orders from an unexpected country)?
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic')
for col in ['sex', 'embarked', 'class', 'who']:
counts = df[col].value_counts(dropna=False)
pct = (counts / len(df) * 100).round(1)
result = pd.DataFrame({'count': counts, 'pct%': pct})
print(f'\n--- {col} ---')
print(result)Bar Charts for Categorical Variables
Visualise categorical value counts as bar charts using sns.countplot or by calling value_counts().plot(kind='bar'). Sort bars from most to least frequent so the viewer can immediately see dominant categories. For binary variables (yes/no, male/female), a pie chart is acceptable — but for more than 3 categories, bar charts are always clearer. Rotate tick labels 45 degrees when category names are long.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset('titanic')
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
sns.countplot(data=df, x='embarked', order=df['embarked'].value_counts().index, ax=axes[0])
axes[0].set_title('Embarkation Port Counts')
sns.countplot(data=df, x='class', order=['First', 'Second', 'Third'], ax=axes[1])
axes[1].set_title('Passenger Class Counts')
plt.tight_layout()
plt.show()Detecting Skewness and Applying Transforms
Highly right-skewed numeric columns (skewness > 1.5) often benefit from a log transform to make them more symmetric. This is important for many statistical tests and some ML algorithms that assume normality. Apply np.log1p() (log(x+1), safe for values near zero) and re-check skewness. Compare histograms before and after to confirm the distribution became more bell-shaped.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = sns.load_dataset('titanic')
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
sns.histplot(df['fare'], bins=30, kde=True, ax=axes[0])
axes[0].set_title(f'fare (skew={df["fare"].skew():.2f})')
log_fare = np.log1p(df['fare'])
sns.histplot(log_fare, bins=30, kde=True, ax=axes[1], color='coral')
axes[1].set_title(f'log1p(fare) (skew={log_fare.skew():.2f})')
plt.tight_layout()
plt.show()Checking for Zero Variance Columns
A column with zero variance (all values are identical) provides no information to any model and should be dropped. A column with near-zero variance (99% of rows have the same value) is similarly useless. Compute variance with df.var() and filter. Also check for columns where nunique() == len(df) — these are likely ID columns that should not be used as features but may be useful as row identifiers.
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic')
numeric = df.select_dtypes('number')
# Zero-variance columns
zero_var = numeric.columns[numeric.var() == 0].tolist()
print('Zero-variance columns:', zero_var)
# Near-zero variance (std < 0.01)
nzv = numeric.columns[numeric.std() < 0.01].tolist()
print('Near-zero variance:', nzv)
# Likely ID columns (all unique values)
id_candidates = [c for c in df.columns if df[c].nunique() == len(df)]
print('Likely ID columns:', id_candidates)Univariate Analysis Automation Loop
Rather than repeating the same analysis manually for each column, write a loop that iterates over all numeric columns and prints key statistics in a structured format. This makes it easy to scan dozens of columns quickly and flag those that need attention. The output can be saved to a CSV file as part of a pipeline audit log that stakeholders can review before data is used in modelling.
import pandas as pd
import seaborn as sns
df = sns.load_dataset('titanic')
numeric = df.select_dtypes('number')
rows = []
for col in numeric.columns:
s = df[col]
Q1, Q3 = s.quantile(0.25), s.quantile(0.75)
IQR = Q3 - Q1
n_outliers = ((s < Q1 - 1.5*IQR) | (s > Q3 + 1.5*IQR)).sum()
rows.append({
'column': col,
'missing_pct': round(s.isna().mean() * 100, 1),
'mean': round(s.mean(), 2),
'std': round(s.std(), 2),
'skew': round(s.skew(), 2),
'outliers': int(n_outliers)
})
report = pd.DataFrame(rows)
print(report.to_string(index=False))Documenting Univariate Findings
After completing univariate analysis, document your findings systematically. For each column note: the distribution shape (skewed, bimodal, normal-ish), missing value percentage and planned imputation strategy, outlier count and decision (cap, remove, flag), and any suspicious values requiring domain clarification. This documentation becomes the data quality log that guides the cleaning steps and protects decisions from being forgotten or contested later.
Quick Check
Test your understanding of univariate analysis from this lesson.
Lesson Recap
In this lesson you learned: histograms reveal distribution shape for numeric columns, box plots and IQR fencing identify outliers, and value_counts reveals category frequency for categorical columns. Automating these checks in a loop creates a reusable quality report. Next up we explore bivariate and correlation analysis — understanding relationships between pairs of columns.
Часто задаваемые вопросы
Урок «Одномерный анализ» бесплатный?
Да — полный текст урока «Одномерный анализ» бесплатно доступен здесь в веб-версии. Чтобы практиковать его интерактивно (встроенный редактор кода и ИИ-репетитор 24/7) и разблокировать остальной курс Pandas & NumPy Academy, подпишись на CoddyKit PRO. Курс Pandas & NumPy Academy содержит 4 уроков всего.
Чему я научусь в уроке «Одномерный анализ»?
Анализируйте каждый столбец независимо: стройте распределения для числовых данных и подсчитывайте значения для категориальных, отмечая выбросы и асимметрию. Ты практикуешь Pandas & NumPy Academy с помощью реального кода, который запускаешь прямо в браузере, и ИИ-репетитор 24/7 отвечает на твои вопросы во время урока.
Нужен ли мне опыт, чтобы начать Pandas & NumPy Academy?
Предыдущий опыт не требуется. Pandas & NumPy Academy на CoddyKit структурирован для всех уровней — от новичков до продвинутых, поэтому ты можешь начать отсюда или с самого начала и учиться в своем темпе. Это урок 2 из 4.
Сколько времени занимает урок «Одномерный анализ»?
Большинство уроков CoddyKit занимают около 5–10 минут. Каждый из них компактный и интерактивный, поэтому ты постоянно делаешь прогресс и продолжаешь с того же места в веб-версии и приложении.
Можно ли писать и запускать код в этом уроке Pandas & NumPy Academy?
Да. Каждый урок Pandas & NumPy Academy включает встроенный редактор кода, поэтому ты пишешь и запускаешь реальный код прямо в браузере и получаешь моментальную обратную связь от AI — локальная установка не требуется.
Все уроки этого курса
- Контрольный список профилирования набора данных
- Одномерный анализ
- Двумерный анализ и анализ корреляций
- Обобщение результатов в отчёте