[Python] Add new columns with variable values

Discussão em 'Python' iniciado por Stack, Fevereiro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I want to add three new columns Version, Year, Month to my Input.xlsx to make it look...

    From this:

    [​IMG]

    To this:

    [​IMG]

    Where;

    • Version is a fixed value i.e. Ver_2145.
    • Year and Month are interrelated. Year should be the current year and Month should be the previous month i.e. for Jan 2025, it would be Dec 2024 shown as M12 2024. For Feb 2025, it would be Jan 2025 displayed as M1 2025 and so on.

    I need help to know how can I pass a variable value for Version, Year and Month below...

    import pandas as pd
    import nump as np
    import datetime

    Version = 'Ver_2145'

    TodayDate = pd.to_datetime('today').normalize().strftime("%Y-%m-%d")
    DateFormat = datetime.datetime.strptime(TodayDate, "%Y-%m-%d")

    Year = DateFormat.Year

    Month = DateFormat.month

    df = pd.read.excel('Input.xlsx', sheet_name='final_data')
    df.insert(3, 'Version', 'Version')
    df.insert(4, 'Year', 'Year')
    df.insert(5, 'Month', 'Month')

    Continue reading...

Compartilhe esta Página