[Python] Create a loop to loop through 2 dataframes and format the dataframes [duplicate]

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a loop in python to loop through 2 data frames df1 and df2. However, I don't seem to be able to achieve this. Below is the code I am using:

    import pandas as pd
    import numpy as np
    import os

    #Variable for input filepath and output filepath
    input_filepath = "C:/Users/elwejam/002_PythonData/002_202_OrgChartData/"
    output_filepath = "C:/Users/elwejam/002_PythonData/000_OutputFolder/"

    df1 = pd.read_csv(input_filepath + file1, skiprows = 12, low_memory=False)
    df2 = pd.read_csv(input_filepath + file2, skiprows = 12, low_memory=False)

    #List of Dataframes
    dataframes = [df1, df2]
    #Loop through each dataframe and set the same parameters
    for df in dataframes:
    #Filter to only necessary columns
    df =df[['Position ID','Position Details','Incumbent']]

    df1.head()


    when I come to looking at df1.head() and df2.head() nothing has changed. There are still 48 columns instead if 3. I am not sure what I am doing incorrectly. Any help or advice would be greatly appreciated.

    Regards,

    ForrestGump1980

    Continue reading...

Compartilhe esta Página