Foundations · Data and Tools for Finance

    Python for Finance (pandas, yfinance, numpy)

    9 min readLast reviewed: July 2025

    Intuition

    Python has become the dominant language for quantitative finance, data analysis, and financial automation. Where Excel reaches its limits, with large datasets, repetitive tasks, complex statistical analysis, or automated data pipelines, Python excels. A financial analyst who can write Python code can automate hours of manual work, analyse datasets too large for Excel, and build reproducible research workflows.

    You don't need to be a software engineer to use Python productively in finance. A working knowledge of three libraries, pandas for data manipulation, numpy for mathematical operations, and matplotlib/seaborn for visualisation, plus the ability to fetch data from APIs or read from files, is sufficient for most financial analysis tasks.

    For Indian markets, the yfinance library provides easy access to NSE price data, and SEBI/exchange APIs provide corporate filing data. Python skills dramatically increase the scope of analysis possible within a given time budget.

    Mechanics

    Core libraries:

    pandas: Data structures (DataFrame, Series) for structured data:

    import pandas as pd
    df = pd.read_csv('results.csv', parse_dates=['date'])
    df['revenue_growth'] = df['revenue'].pct_change()
    df.groupby('sector')['ebitda_margin'].mean()
    

    numpy: Numerical operations:

    import numpy as np
    npv = np.npv(0.12, [-1000, 300, 400, 500, 400])  # Net Present Value
    irr = np.irr([-1000, 300, 400, 500, 400])          # IRR
    

    yfinance: Price and fundamental data:

    import yfinance as yf
    reliance = yf.Ticker("RELIANCE.NS")
    history = reliance.history(period="5y")
    info = reliance.info  # P/E, market cap, sector
    

    matplotlib / seaborn: Visualisation:

    import matplotlib.pyplot as plt
    df['revenue'].plot(kind='bar', title='Revenue Trend')
    plt.tight_layout()
    plt.savefig('revenue_chart.png')
    

    Useful for finance:

    • Downloading multi-year price history and computing returns
    • Screener scraping with requests/BeautifulSoup (check ToS)
    • Building discounted cash flow calculators
    • Regression analysis (statsmodels, scikit-learn)
    • Portfolio optimisation (scipy.optimize)

    Try it yourself

    Interactive exercises coming soon.

    Related topics

    Stay in the loop

    Roughly one email per month. No spam, no upsells.