How to Filter Excel Data with Pandas: A Step‑by‑Step Guide
This article walks through a real‑world pandas example that filters an Excel sheet by department and ID length, shows the complete Python code, displays the resulting output, and explains how the solution resolved a reader's data‑processing question.
Hello, I'm PiPi.
1. Introduction
A few days ago a member of the Python community asked a question about using pandas to process Excel data. The original spreadsheet contains mixed Chinese and English column names, but the task is to extract rows where the "DEPT" column equals "德语系" and the "CERT_ID_F" column has a length of 10 characters.
The raw data (fields omitted for brevity) looks like this:
2. Implementation
After several attempts, the correct code was produced:
import pandas as pd
df = pd.read_excel("借阅记录.xlsx")
df1 = df[(df['DEPT'] == '德语系') & (df['CERT_ID_F'].str.len() == 10)]
print(df1)Running the script yields the following filtered result:
This output satisfies the original request and demonstrates how to combine column value matching with string‑length filtering in pandas.
3. Conclusion
The article presented a concrete pandas solution for the Excel filtering problem, including the full code and resulting data. It illustrates a practical technique that can be adapted to similar data‑cleaning tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
