[Python] Using mangle_dupe_cols with pandas to simplify read_csv

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    Goal here is to read a .csv file, take the information I need from it, and output a new .csv file with the specific information that I need. The mangle_dupe_cols parameter keeps erroring with "not supported yet" and I know this would help me simplify things. As you can see, I want to discard a lot of columns within the dataset and then rename all the ones that are left.

    import pandas as pd

    columns_to_skip = ['W','L','D','-','!','+','#%','Ind','E%','/','-.1','-.2','-.3','-.4','-.5','-.6','+.1','+.2','+.3','+.4','+.5','+.6','!.1','!.2','!.3','!.4','!.5','!.6','Ind.1','Ind.2','Ind.3','Ind.4','Ind.5','Ind.6','E%.1','E%.2','E%.3','E%.4','E%.5','E%.6','#%.1','#%.2','#%.3','#%.4','#%.5','#%.6']

    df = pd.read_csv('P65 Stats.csv', header=3, usecols = lambda x: x not in columns_to_skip)

    df = df.rename(columns = {'Unnamed: 0':'Name'})
    df = df.rename(columns = {'Tot':'Total Serves'})
    df = df.rename(columns = {'Tot.1':'Total Passes'})
    df = df.rename(columns = {'Tot.2':'Total Attacks'})
    df = df.rename(columns = {'Tot.3':'Total Blocks'})
    df = df.rename(columns = {'Tot.4':'Total Digs'})
    df = df.rename(columns = {'Tot.5':'Total FB Passes'})
    df = df.rename(columns = {'Tot.6':'Total Sets'})
    df = df.rename(columns = {'=':'Errors'})
    df = df.rename(columns = {'=.1':'Errors'})
    df = df.rename(columns = {'=.2':'Errors'})
    df = df.rename(columns = {'=.3':'Errors'})
    df = df.rename(columns = {'=.4':'Errors'})
    df = df.rename(columns = {'=.5':'Errors'})
    df = df.rename(columns = {'=.6':'Errors'})
    df = df.rename(columns = {'#':'Aces'})
    df = df.rename(columns = {'#.1':'Kills'})
    df = df.rename(columns = {'#.2':'Stuff Blocks'})
    df = df.rename(columns = {'#.3':'Perfect Digs'})

    print(df)


    Any help would be great!

    Continue reading...

Compartilhe esta Página