Created
September 14, 2022 20:21
-
-
Save x/54a5379e06aa3fff0bcae15b191021c4 to your computer and use it in GitHub Desktop.
Revisions
-
x created this gist
Sep 14, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()