Extracting Tables from Word Documents into Pandas Data Frames: A Key Skill for AI/ML Workflows
As an artificial intelligence and machine learning expert, I frequently emphasize to aspiring practitioners that data preparation is one of the most crucial yet time-consuming aspects of real-world AI/ML projects. A 2020 survey by Anaconda found that data scientists spend 45% of their time on data preparation tasks, such as loading, cleaning, and transforming data (source).
Extracting tabular data from diverse sources and shaping it into a format suitable for analysis and modeling is a key component of data preparation. Microsoft Word documents are a common source of valuable data, particularly in business contexts. Therefore, being able to efficiently extract tables from Word docs and load them into pandas data frames is an essential skill.
In this guide, we‘ll walk through a complete example of reading tables from a Word document into pandas data frames using Python, while explaining the benefits of this approach from an AI/ML perspective. We‘ll also cover some advanced pandas techniques for further preparing the tabular data for machine learning.
By the end, you‘ll appreciate why this data engineering task is a core part of an AI/ML workflow, and will have the knowledge to apply it effectively on your own data science projects.
Why Data Frames are Essential for Machine Learning
Before diving into the technical details of reading Word tables into data frames, let‘s consider more deeply why data frames are such an essential abstraction for AI/ML.
A data frame is a two-dimensional labeled data structure with columns of potentially different types, similar to an spreadsheet or SQL table. Pandas is the most popular Python library for working with data frames, and is a foundational part of the data science stack in Python, along with tools for machine learning like scikit-learn and TensorFlow.
According to the Stack Overflow 2022 Developer Survey, pandas is the 5th most popular Python library overall, used by 69% of Python developers (source). Its widespread adoption in the data science community is due to its powerful features for data manipulation, analysis, and preparation.
From a machine learning perspective, data frames serve several vital roles:
-
Standardizing input data: ML models expect input data in a specific format, typically a 2D numeric array where rows are samples and columns are features. Data frames provide a convenient structure to hold this "tabular" data, which can originate from diverse sources like CSV files, SQL databases, or in our case, Word documents.
-
Exploring and transforming data: Data frames make it easy to examine the structure, statistics, and relationships within the data, which is crucial for feature engineering – the process of selecting and transforming input variables to optimize model performance. Pandas provides a rich set of functions for filtering, grouping, aggregating, joining, and reshaping data.
-
Handling messy data: Real-world data is often incomplete, inconsistent, and noisy. Data frames can efficiently represent missing values, and pandas provides tools for imputing or removing them. They also enable cleaning tasks like removing duplicates, fixing inconsistent values, and dealing with outliers.
-
Integrating with ML libraries: The output of data preparation in pandas is typically a cleaned, transformed data frame ready to be converted to a NumPy array or TensorFlow Dataset and fed into a ML model. Pandas interoperates seamlessly with the rest of the PyData ecosystem.
In essence, data frames serve as the lingua franca for data in Python-based machine learning projects. By expressing our input data as data frames, we can leverage the powerful APIs of pandas and scikit-learn to streamline data preparation and model training.
Reading Word Tables into Data Frames
With an understanding of why data frames are so valuable, let‘s proceed with our example of reading tables from a Word document and converting them to pandas data frames.
We‘ll use the python-docx library to read a Word document and extract its tables. Here are the key steps:
- Install
python-docxand pandas using pip:
pip install python-docx pandas
- Open the Word document:
from docx import Document
doc = Document(‘example.docx‘)
- Access the tables in the document:
tables = doc.tables
- Convert each table to a data frame:
import pandas as pd
def table_to_df(table):
data = []
for i, row in enumerate(table.rows):
text = (cell.text for cell in row.cells)
if i == 0:
colnames = tuple(text)
else:
data.append(dict(zip(colnames, text)))
return pd.DataFrame(data)
dfs = {}
for i, table in enumerate(tables):
dfs[f‘table_{i}‘] = table_to_df(table)
The table_to_df function does the heavy lifting of extracting the text content from each table cell and using it to construct a list of row dictionaries, which can be passed to the DataFrame constructor. We apply it to each table found in the document, storing the resulting data frames in a dictionary keyed by table index.
Here‘s what one of the extracted data frames might look like:
>>> print(dfs[‘table_0‘])
Category Value
0 A 10
1 B 20
2 C 30
This data is now in a highly usable format for further exploration and transformation using pandas, as we‘ll see next.
Preparing Data Frames for Machine Learning
With our Word tables now loaded as data frames, we can leverage pandas to perform additional data preparation steps that are commonly needed for machine learning. Here are a few examples.
Handling Missing Data
One of the first steps in preparing data for modeling is dealing with missing values, as most ML algorithms do not handle them natively. Pandas represents missing data as NaN (not a number) and provides functions for detecting, filtering, and imputing missing values.
For example, to check for missing values in our data frame:
>>> print(dfs[‘table_0‘].isnull().sum())
Category 0
Value 0
dtype: int64
If there were missing values, we could remove rows containing them using dropna():
df = dfs[‘table_0‘].dropna()
Alternatively, we can impute missing values, such as filling them with a specified value like 0 or with the mean of the column:
df = dfs[‘table_0‘].fillna(0)
df = dfs[‘table_0‘].fillna(dfs[‘table_0‘].mean())
The most appropriate handling of missing values will depend on the nature of your data and specific modeling task.
Encoding Categorical Variables
Another common data preparation task is converting categorical variables (discrete values in a specific set of possible values) to numeric representations for use in ML models.
Pandas supports transforming categorical data in several ways. For example, we can use one-hot encoding to convert each category to a new binary column:
>>> print(pd.get_dummies(dfs[‘table_0‘]))
Value Category_A Category_B Category_C
0 10 1 0 0
1 20 0 1 0
2 30 0 0 1
This is a useful encoding for some ML algorithms like decision trees and random forests.
Another option is label encoding, which assigns each unique category value an integer:
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df[‘Category‘] = le.fit_transform(df[‘Category‘])
>>> print(df)
Category Value
0 0 10
1 1 20
2 2 30
Label encoding is often used for ordinal variables where the integer values have an inherent order.
Scaling Numeric Features
For ML algorithms that are sensitive to the scale of input features, such as support vector machines, linear models, and neural networks, it‘s important to normalize or standardize numeric variables so they have similar magnitudes.
Pandas can integrate with scikit-learn transformers to scale data. For instance, here‘s how to apply Min-Max scaling to transform the ‘Value‘ column to a range between 0 and 1:
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df[‘Value‘] = scaler.fit_transform(df[[‘Value‘]])
>>> print(df)
Category Value
0 0 0.000000
1 1 0.500000
2 2 1.000000
Alternatively, standardization scales the variable to have zero mean and unit variance:
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
df[‘Value‘] = scaler.fit_transform(df[[‘Value‘]])
>>> print(df)
Category Value
0 0 -1.224745
1 1 0.000000
2 2 1.224745
Feature scaling is a crucial preprocessing step to ensure that all input features contribute proportionately to the model‘s learning process.
Integrating with Machine Learning Libraries
The ultimate goal of loading and preparing our Word table data in pandas is to feed it into a machine learning model. Pandas makes this easy by providing seamless integration with popular Python ML libraries.
For example, suppose we wanted to train a simple linear regression model to predict the ‘Value‘ column from the ‘Category‘ column in our table data. Here‘s how we can achieve that using scikit-learn:
from sklearn.linear_model import LinearRegression
X = df[[‘Category‘]]
y = df[‘Value‘]
model = LinearRegression()
model.fit(X, y)
print(model.coef_, model.intercept_)
This code fits a linear regression model using the ‘Category‘ feature to predict the ‘Value‘. The trained model‘s coefficients and intercept are printed.
We can evaluate the model‘s performance on a hold-out test set like this:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
This splits the data into training and test sets, trains the model on the training set, and prints the R-squared score on the test set.
In a real ML workflow, we would likely perform more extensive feature engineering, model selection, and hyperparameter tuning. But this example illustrates how we can flow tabular data from Word through pandas into a scikit-learn model with just a few lines of code.
We can similarly integrate with deep learning libraries like TensorFlow or PyTorch, or with specialized tools like spaCy for natural language processing or OpenCV for computer vision, to build all sorts of AI/ML applications powered by data originally locked in Word documents.
Conclusion
In this article, we‘ve taken a deep dive into the process of extracting tables from Microsoft Word documents and converting them into pandas data frames, a vital skill for data preparation in machine learning and AI projects.
We walked through a complete example using python-docx to read a Word file and parse its tables, and pandas to create data frames from the extracted tabular data.
We then explored several common data preprocessing techniques using pandas, including handling missing values, encoding categorical variables, and scaling numeric features, all of which are essential for preparing data for machine learning models.
Finally, we saw how easy it is to integrate pandas data frames with scikit-learn and other ML libraries to train and evaluate models.
By mastering these data engineering skills, you‘ll be well-equipped to tackle the data preparation challenges that are central to successful machine learning projects. You‘ll be able to efficiently extract insights from diverse tabular data sources, wrangle messy data into a form suitable for modeling, and leverage the power of the PyData ecosystem to build performant AI/ML systems.
While we focused on Word documents as a source of tabular data, the same principles and techniques apply to data ingestion from spreadsheets, CSV files, databases, APIs, and many other sources. By building fluency with pandas data frames as the common currency of data in Python, you‘ll be ready to handle data preparation for machine learning wherever your data lives.
References and Further Reading
python-docxdocumentation: https://python-docx.readthedocs.io- pandas documentation: https://pandas.pydata.org/docs/
- "Tidy Data", Hadley Wickham: https://www.jstatsoft.org/article/view/v059i10
- "Python for Data Analysis", Wes McKinney: https://www.oreilly.com/library/view/python-for-data/9781098104023/
- scikit-learn documentation: https://scikit-learn.org/