christinarobinson1999
christinarobinson1999 7d ago β€’ 20 views

Sample code for Reading and Filtering DataFrames in Pandas

Hey eokultv! πŸ‘‹ I'm really struggling with Pandas DataFrames. How do I actually *read* data into them from files like CSVs, and then how do I *filter* out just the rows and columns I need? It feels a bit overwhelming with all the different methods. Can you give me some clear code examples? πŸ™
πŸ’» Computer Science & Technology
πŸͺ„

πŸš€ Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

βœ… Best Answer
User Avatar
collins.sarah22 Mar 21, 2026

πŸ“š Understanding Pandas DataFrames: Reading and Filtering Essentials

Pandas is an open-source data manipulation and analysis library for Python. It provides data structures like DataFrames and Series, which are designed to make working with "relational" or "labeled" data both easy and intuitive. Learning to read data into a DataFrame and then efficiently filter it is fundamental for any data analysis task, allowing you to focus on relevant subsets of your information.

πŸ“œ The Genesis of DataFrames: A Brief History of Pandas

  • πŸ’‘ Pandas was developed by Wes McKinney in 2008 while he was at AQR Capital Management, primarily to address the need for a high-performance, flexible tool for quantitative analysis in Python.
  • πŸ“ˆ The name "Pandas" is derived from the term "Panel Data," an econometrics term for datasets that include observations over multiple time periods for the same individuals.
  • πŸš€ Since its open-sourcing in 2009, Pandas has grown to become one of the most widely used libraries in the data science ecosystem, foundational for data cleaning, transformation, and analysis.

πŸ” Core Principles of DataFrame Interaction

  • πŸ’Ύ Data Ingestion: DataFrames can be populated from various sources, including CSV, Excel, SQL databases, JSON, and more, using read_ functions.
  • 🎯 Selection by Label (.loc): This method selects data rows and columns based on their labels (index names and column names). It's inclusive of the end label.
  • πŸ”’ Selection by Position (.iloc): This method selects data rows and columns based on their integer-location (from 0 to length-1). It's exclusive of the end position.
  • βœ… Boolean Indexing: A powerful filtering technique where you create a boolean Series (True/False) based on a condition, and then use this Series to select rows where the condition is True.
  • πŸ”— Chaining Operations: Pandas allows for method chaining, enabling concise and readable data manipulation workflows.

πŸ› οΈ Practical Examples: Reading and Filtering DataFrames

πŸ“‚ Reading Data into a DataFrame

The first step in any data analysis workflow is to load your data. Pandas offers a variety of functions for this purpose, depending on your data source.

  • πŸ“„ From a CSV file: This is one of the most common ways to load tabular data.
    import pandas as pd
    
    # Create a dummy CSV file for demonstration
    data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],
            'Age': [24, 27, 22, 32],
            'City': ['New York', 'Los Angeles', 'Chicago', 'Houston'],
            'Salary': [70000, 80000, 60000, 95000]}
    df_dummy = pd.DataFrame(data)
    df_dummy.to_csv('employees.csv', index=False)
    
    df_csv = pd.read_csv('employees.csv')
    print("DataFrame from CSV:")
    print(df_csv)
  • πŸ“Š From an Excel file: Similar to CSV, but for Excel spreadsheets. Requires openpyxl to be installed (pip install openpyxl).
    # Create a dummy Excel file
    df_dummy.to_excel('employees.xlsx', index=False)
    
    df_excel = pd.read_excel('employees.xlsx')
    print("\nDataFrame from Excel:")
    print(df_excel)
  • πŸ—„οΈ From a SQL database: Connect to a database and read a table or query result. Requires a database connector (e.g., sqlite3 for SQLite, psycopg2 for PostgreSQL).
    import sqlite3
    
    # Create a dummy SQLite database
    conn = sqlite3.connect('employees.db')
    df_dummy.to_sql('employees', conn, if_exists='replace', index=False)
    
    df_sql = pd.read_sql('SELECT * FROM employees WHERE Age > 25', conn)
    print("\nDataFrame from SQL (filtered):")
    print(df_sql)
    conn.close()

➑️ Filtering DataFrames: Selecting Rows and Columns

Once your data is loaded, filtering allows you to select specific subsets based on conditions.

  • πŸ“ Basic Boolean Indexing (Single Condition): Select rows where a specific column meets a condition.
    # Using the df_csv DataFrame
    print("\nEmployees older than 25:")
    df_older_than_25 = df_csv[df_csv['Age'] > 25]
    print(df_older_than_25)
  • βž• Multiple Conditions (AND/OR): Combine conditions using & (AND) or | (OR). Remember to use parentheses for each condition.
    print("\nEmployees older than 25 AND living in New York:")
    df_filtered_and = df_csv[(df_csv['Age'] > 25) & (df_csv['City'] == 'New York')]
    print(df_filtered_and)
    
    print("\nEmployees living in New York OR Chicago:")
    df_filtered_or = df_csv[(df_csv['City'] == 'New York') | (df_csv['City'] == 'Chicago')]
    print(df_filtered_or)
  • 🏷️ Filtering with .loc (Label-based): Excellent for selecting rows by label and specific columns by label.
    # Select rows where Age > 25 and only 'Name' and 'City' columns
    print("\nEmployees older than 25 (using .loc, specific columns):")
    df_loc_filtered = df_csv.loc[df_csv['Age'] > 25, ['Name', 'City']]
    print(df_loc_filtered)
    
    # Select a specific row by index label and all columns
    print("\nEmployee at index 1 (using .loc):")
    print(df_csv.loc[1])
  • πŸ“ Filtering with .iloc (Position-based): Useful when you need to select rows/columns by their integer position.
    # Select the first two rows and the first two columns (Name, Age)
    print("\nFirst two employees, first two columns (using .iloc):")
    df_iloc_filtered = df_csv.iloc[0:2, 0:2]
    print(df_iloc_filtered)
    
    # Select all rows and the column at index 2 (City)
    print("\nAll employees, 'City' column (using .iloc):")
    print(df_csv.iloc[:, 2])
  • πŸ’¬ Filtering String Columns: Use .str accessor for string operations like contains, startswith, endswith.
    print("\nEmployees whose city starts with 'N':")
    df_city_N = df_csv[df_csv['City'].str.startswith('N')]
    print(df_city_N)
    
    print("\nEmployees whose name contains 'a' (case-insensitive):")
    df_name_a = df_csv[df_csv['Name'].str.contains('a', case=False)]
    print(df_name_a)
  • 🚫 Handling Missing Data (NaN): Filter out or identify rows with missing values.
    # Add a row with some missing data for demonstration
    df_with_nan = pd.DataFrame({'Name': ['Eve', 'Frank'],
                                'Age': [29, None],
                                'City': [None, 'Seattle'],
                                'Salary': [85000, 72000]})
    df_csv_nan = pd.concat([df_csv, df_with_nan], ignore_index=True)
    print("\nDataFrame with NaN values:")
    print(df_csv_nan)
    
    print("\nRows with any missing values:")
    df_missing_rows = df_csv_nan[df_csv_nan.isnull().any(axis=1)]
    print(df_missing_rows)
    
    print("\nRows with NO missing values:")
    df_no_missing = df_csv_nan.dropna() # or df_csv_nan[df_csv_nan.notnull().all(axis=1)]
    print(df_no_missing)

🎯 Conclusion: Mastering Data Access and Refinement

Reading and filtering DataFrames are indispensable skills in your data science toolkit. By understanding the various read_ functions, and mastering boolean indexing, .loc, and .iloc, you gain precise control over your data. These techniques allow you to efficiently prepare your datasets for deeper analysis, visualization, and machine learning model building. Keep practicing with different datasets to solidify your understanding and become a Pandas pro!

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€