Skip to content

Instantly share code, notes, and snippets.

@x
Created September 14, 2022 20:21
Show Gist options
  • Select an option

  • Save x/54a5379e06aa3fff0bcae15b191021c4 to your computer and use it in GitHub Desktop.

Select an option

Save x/54a5379e06aa3fff0bcae15b191021c4 to your computer and use it in GitHub Desktop.

Revisions

  1. x created this gist Sep 14, 2022.
    35 changes: 35 additions & 0 deletions extract_and_merge_csv_cols.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!python
    """
    extract_and_merge_csv_cols.py is a script to extract a target column from a set
    of csvs and merge them into a single csv where each column name is the filename
    of the original csv.
    Usage:
    extract_and_merge_csv_cols.py <target_col> <csvs>...
    """

    import argparse

    import pandas as pd


    def main():
    parser = argparse.ArgumentParser(
    description="Extract a target column from a set of csvs and merge them into a single CSV."
    )
    parser.add_argument("target_col", help="The target column to extract")
    parser.add_argument("csvs", nargs="+", help="The csvs to extract from")
    args = parser.parse_args()

    dfs = []
    for csv in args.csvs:
    df = pd.read_csv(csv)
    df = df[[args.target_col]].rename(columns={args.target_col: csv})
    dfs.append(df)

    df = pd.concat(dfs, axis=1)
    df.to_csv("merged.csv", index=False)


    if __name__ == "__main__":
    main()