Payment Currency Options
Steps in Descriptive Data Analysis Using Python
Descriptive analysis is the process of summarising, organising and presenting data so that researchers can understand the main characteristics of a dataset. In Python, descriptive analysis can be conducted efficiently using libraries such as Pandas, NumPy, SciPy and Matplotlib.
Below is a practical step-by-step procedure suitable for students, researchers, NGOs and organisations.
1. Install the Required Python Libraries
The commonly used libraries are:
pip install pandas numpy scipy matplotlib seaborn openpyxlThe main purposes are:
- Pandas – data management and descriptive statistics
- NumPy – numerical calculations
- SciPy – statistical analysis
- Matplotlib – graphs and charts
- Seaborn – statistical visualisations
- Openpyxl – reading and writing Excel files
2. Import the Libraries
Start your Python analysis by importing the required packages.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats3. Import Your Dataset
For an Excel dataset:
data = pd.read_excel("research_data.xlsx")For a CSV dataset:
data = pd.read_csv("research_data.csv")You can then display the first observations:
data.head()To display the last observations:
data.tail()4. Examine the Structure of the Dataset
Before conducting analysis, determine the size and structure of your data.
data.shapeThis returns:
(number of rows, number of columns)For example:
(250, 15)means that the dataset contains 250 observations and 15 variables.
You can also examine the variable names:
data.columns5. Check the Data Types
Use:
data.info()This helps identify whether variables are:
- Integer
- Float
- Text/string
- Date/time
- Boolean
Correct data types are important because statistical calculations depend on the type of variable.
6. Check for Missing Values
Missing data can affect your analysis.
Use:
data.isnull().sum()To calculate the percentage of missing observations:
data.isnull().mean() * 100For example:
| Variable | Missing |
|---|---|
| Age | 2 |
| Income | 5 |
| Education | 0 |
| Gender | 1 |
You can then decide whether to exclude, replace or otherwise handle missing observations based on the research design.
7. Clean the Data
Before calculating descriptive statistics, check for errors and inconsistencies.
For example:
data["Age"].describe()If you find impossible values such as an age of 250, investigate the original questionnaire or dataset.
You can also remove duplicate observations:
data = data.drop_duplicates()8. Generate Frequencies for Categorical Variables
Frequency analysis is commonly used for variables such as:
- Gender
- Education level
- Marital status
- Occupation
- Enterprise type
- Location
For example:
data["Gender"].value_counts()To obtain percentages:
data["Gender"].value_counts(normalize=True) * 100You can combine frequencies and percentages:
frequency = data["Gender"].value_counts()
percentage = data["Gender"].value_counts(normalize=True) * 100
result = pd.DataFrame({
"Frequency": frequency,
"Percentage": percentage
})
print(result)9. Calculate Measures of Central Tendency
Descriptive analysis normally includes measures of central tendency:
Mean
data["Income"].mean()Median
data["Income"].median()Mode
data["Income"].mode()The mean is the arithmetic average, while the median represents the middle observation when values are ordered. The mode is the most frequently occurring value.
10. Calculate Measures of Dispersion
You can calculate the spread of the observations using:
Minimum
data["Income"].min()Maximum
data["Income"].max()Range
data["Income"].max() - data["Income"].min()Variance
data["Income"].var()Standard deviation
data["Income"].std()Standard deviation is particularly useful for understanding how widely observations vary around the mean.
11. Generate a Complete Descriptive Statistics Table
Pandas provides a convenient command:
data.describe()For numerical variables, this produces:
- Count
- Mean
- Standard deviation
- Minimum
- 25th percentile
- Median
- 75th percentile
- Maximum
For example:
Age Income
count 100.0 100.0
mean 38.4 850000.0
std 9.2 320000.0
min 20.0 250000.0
25% 31.0 600000.0
50% 37.0 800000.0
75% 45.0 1000000.0
max 67.0 2500000.012. Analyse Categorical Variables
You can generate descriptive information for categorical variables using:
data["Education"].value_counts()For several categorical variables:
categorical_variables = ["Gender", "Education", "Marital_Status"]
for variable in categorical_variables:
print("\n", variable)
print(data[variable].value_counts())13. Analyse Numerical Variables
Select numerical variables:
numeric_data = data.select_dtypes(include=np.number)Then:
numeric_data.describe()This provides a summary of all numerical variables.
14. Examine Relationships Between Variables
Although descriptive analysis primarily summarises individual variables, researchers often examine simple relationships between numerical variables.
For example, correlation:
data["Income"].corr(data["Age"])For correlations among several variables:
data.corr(numeric_only=True)A correlation matrix can also be visualised:
plt.figure(figsize=(10, 7))
sns.heatmap(data.corr(numeric_only=True), annot=True)
plt.title("Correlation Matrix")
plt.show()15. Create Frequency Charts
A bar chart can be used to present categorical data.
data["Gender"].value_counts().plot(kind="bar")
plt.title("Distribution of Respondents by Gender")
plt.xlabel("Gender")
plt.ylabel("Frequency")
plt.show()16. Create a Histogram
Histograms are useful for examining the distribution of numerical variables.
For example:
plt.hist(data["Age"], bins=10)
plt.title("Distribution of Respondents by Age")
plt.xlabel("Age")
plt.ylabel("Frequency")
plt.show()17. Create a Pie Chart
For simple categorical distributions:
data["Gender"].value_counts().plot(
kind="pie",
autopct="%1.1f%%"
)
plt.title("Respondents by Gender")
plt.ylabel("")
plt.show()For academic research, however, bar charts are often easier to interpret than pie charts, particularly when there are many categories.
18. Create a Boxplot
A boxplot helps identify the distribution and potential outliers.
sns.boxplot(x=data["Income"])
plt.title("Distribution of Income")
plt.show()This can help researchers identify unusually high or low observations that require investigation.
19. Cross-Tabulation
Cross-tabulation is useful when examining two categorical variables.
For example, gender and education:
pd.crosstab(
data["Gender"],
data["Education"]
)To display row percentages:
pd.crosstab(
data["Gender"],
data["Education"],
normalize="index"
) * 100This can be useful in survey research.
20. Export Results to Excel
After producing your descriptive statistics, you can export them to Excel.
descriptive = data.describe()
descriptive.to_excel("descriptive_statistics.xlsx")You can also export frequency tables:
frequency.to_excel("gender_frequency.xlsx")21. Interpret the Results
Generating statistics is only one part of research analysis. The researcher must also interpret what the statistics mean.
For example, suppose the analysis produces:
- Mean age = 38.4 years
- Median age = 37 years
- Standard deviation = 9.2 years
- Minimum = 20 years
- Maximum = 67 years
A research report might state:
The findings indicate that respondents had a mean age of 38.4 years, with a standard deviation of 9.2 years. The youngest respondent was 20 years old, while the oldest was 67 years old, indicating variation in the age distribution of respondents.
The interpretation should be based on the actual results rather than simply describing Python commands.
22. Present the Results in a Research Report
A typical descriptive-analysis section may contain:
Table 1: Demographic characteristics
| Variable | Frequency | Percentage |
|---|---|---|
| Male | 60 | 60.0 |
| Female | 40 | 40.0 |
| Total | 100 | 100.0 |
Table 2: Descriptive statistics
| Variable | Mean | Std. Deviation | Minimum | Maximum |
|---|---|---|---|---|
| Age | 38.4 | 9.2 | 20 | 67 |
| Income | 850,000 | 320,000 | 250,000 | 2,500,000 |
The tables can then be followed by a written interpretation explaining the major findings.
Complete Python Example
The following is a simple workflow that can be adapted to a research dataset:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 1. Import data
data = pd.read_excel("research_data.xlsx")
# 2. Inspect data
print(data.head())
print(data.shape)
print(data.info())
# 3. Check missing values
print(data.isnull().sum())
# 4. Descriptive statistics
print(data.describe())
# 5. Gender frequency
gender_frequency = data["Gender"].value_counts()
gender_percentage = data["Gender"].value_counts(normalize=True) * 100
gender_table = pd.DataFrame({
"Frequency": gender_frequency,
"Percentage": gender_percentage
})
print(gender_table)
# 6. Mean, median and standard deviation
print("Mean age:", data["Age"].mean())
print("Median age:", data["Age"].median())
print("Standard deviation:", data["Age"].std())
# 7. Frequency chart
data["Gender"].value_counts().plot(kind="bar")
plt.title("Distribution of Respondents by Gender")
plt.xlabel("Gender")
plt.ylabel("Frequency")
plt.show()
# 8. Histogram
plt.hist(data["Age"], bins=10)
plt.title("Distribution of Respondents by Age")
plt.xlabel("Age")
plt.ylabel("Frequency")
plt.show()
# 9. Correlation matrix
correlation = data.corr(numeric_only=True)
print(correlation)
# 10. Export descriptive statistics
data.describe().to_excel("descriptive_statistics.xlsx")Recommended Research Workflow
For a student, NGO or organisational research project, the overall Python workflow can therefore be structured as:
Import data → Inspect data → Clean data → Handle missing values → Code variables → Generate frequencies → Calculate percentages → Calculate mean/median/mode → Calculate standard deviation → Examine distributions → Generate cross-tabulations → Create charts → Export tables → Interpret findings → Write the results chapter.
This workflow can be extended from simple descriptive statistics to correlation, t-tests, ANOVA, chi-square tests and regression analysis when these techniques are appropriate for the research objectives and data.