Skip to content

Instantly share code, notes, and snippets.

@x
Created September 14, 2022 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x/54a5379e06aa3fff0bcae15b191021c4 to your computer and use it in GitHub Desktop.
Save x/54a5379e06aa3fff0bcae15b191021c4 to your computer and use it in GitHub Desktop.
#!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()
@x
Copy link
Author

x commented Sep 14, 2022

$ cat foo.csv 
foo,bar,baz
1,2,3
4,5,6
7,8,9

$ cat bar.csv 
foo,bar,baz
7,8,9
4,5,6
1,2,3

$ ./extract_and_merge_csv_cols.py foo foo.csv bar.csv

$ cat merged.csv 
foo.csv,bar.csv
1,7
4,4
7,1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment