help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
polars-fastdataframes
/
scripts
RSK World
polars-fastdataframes
High-performance DataFrames with Polars
scripts
  • __pycache__
  • advanced_queries.py7 KB
  • basic_operations.py3 KB
  • data_generator.py4.2 KB
  • lazy_evaluation.py3.2 KB
  • performance_comparison.py7.3 KB
basic_operations.py
scripts/basic_operations.py
Raw Download
Find: Go to:
"""
Basic Polars DataFrame Operations
Demonstrates fundamental operations with Polars DataFrames

Author: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
"""

import polars as pl
import numpy as np
from datetime import datetime, timedelta

def demonstrate_basic_operations():
    """Demonstrate basic Polars DataFrame operations"""
    
    print("=" * 60)
    print("BASIC POLARS DATAFRAME OPERATIONS")
    print("=" * 60)
    
    # Create a sample DataFrame
    print("\n1. Creating a DataFrame:")
    df = pl.DataFrame({
        'id': [1, 2, 3, 4, 5],
        'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
        'age': [25, 30, 35, 28, 32],
        'salary': [50000, 60000, 70000, 55000, 65000],
        'department': ['IT', 'HR', 'IT', 'Finance', 'IT']
    })
    print(df)
    
    # Select columns
    print("\n2. Selecting columns:")
    selected = df.select(['name', 'age', 'salary'])
    print(selected)
    
    # Filter rows
    print("\n3. Filtering rows (age > 30):")
    filtered = df.filter(pl.col('age') > 30)
    print(filtered)
    
    # Add new column
    print("\n4. Adding a new column:")
    df_with_bonus = df.with_columns([
        (pl.col('salary') * 0.1).alias('bonus')
    ])
    print(df_with_bonus)
    
    # Group by and aggregate
    print("\n5. Group by and aggregate:")
    grouped = df.group_by('department').agg([
        pl.col('salary').mean().alias('avg_salary'),
        pl.col('age').mean().alias('avg_age'),
        pl.count().alias('count')
    ])
    print(grouped)
    
    # Sort
    print("\n6. Sorting by salary (descending):")
    sorted_df = df.sort('salary', descending=True)
    print(sorted_df)
    
    # Join operations
    print("\n7. Join operations:")
    dept_df = pl.DataFrame({
        'department': ['IT', 'HR', 'Finance'],
        'location': ['Building A', 'Building B', 'Building C']
    })
    joined = df.join(dept_df, on='department', how='left')
    print(joined)
    
    # Window functions
    print("\n8. Window functions (rank by salary):")
    ranked = df.with_columns([
        pl.col('salary').rank().over('department').alias('rank_in_dept')
    ])
    print(ranked)
    
    # String operations
    print("\n9. String operations (uppercase names):")
    string_ops = df.with_columns([
        pl.col('name').str.to_uppercase().alias('name_upper')
    ])
    print(string_ops)
    
    # Date operations
    print("\n10. Date operations:")
    dates_df = pl.DataFrame({
        'date': pl.date_range(datetime(2023, 1, 1), datetime(2023, 1, 10), '1d', eager=True),
        'value': range(10)
    })
    dates_df = dates_df.with_columns([
        pl.col('date').dt.year().alias('year'),
        pl.col('date').dt.month().alias('month'),
        pl.col('date').dt.day().alias('day')
    ])
    print(dates_df)
    
    print("\n" + "=" * 60)
    print("Basic operations demonstration complete!")
    print("=" * 60)

if __name__ == "__main__":
    demonstrate_basic_operations()

107 lines•3 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer