Recursively Edit CSV to Subdirectories using Pandas

后端 未结 1 1053
臣服心动
臣服心动 2021-01-27 12:18

I have a series of subdirectory folders that each have a \"_Invoice.csv\".

/Invoice List/
              Invoice1folder/
                             ..._Invoice.         


        
相关标签:
1条回答
  • 2021-01-27 13:04

    This will do the job.

    Instead of opening, removing columns, saving and moving on; I have opted for opening only with the reduced columns, saving this reduced DataFrame, then appending to df. This will result in all the reduced files being stacked in this one DataFrame.

    Using path = "." goes from the current directory

    from pathlib import Path
    import pandas as pd
    
    
    df = pd.DataFrame()
    columns_to_keep = ['A','C']
    path = "."
    pattern = "*_Invoice.csv"
    
    for file in Path(path).rglob(pattern):
        output_file = "{}/{}{}".format(file.parent, file.stem, "_Reduced.csv")
        _df = pd.read_csv(file, usecols=columns_to_keep)
        _df.to_csv(output_file, sep=",", index=False, header=True)
        df = pd.concat([df, _df])
    
    0 讨论(0)
提交回复
热议问题