-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathconcat_csv.py
More file actions
28 lines (24 loc) · 875 Bytes
/
concat_csv.py
File metadata and controls
28 lines (24 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/python
import argparse
import pandas as pd
parser = argparse.ArgumentParser(
description="Small utility to safely concatenate multiple csv files, "
"especially useful here when concatenating multiple `results.csv` files.",
epilog="""Usage example:
find ./results -name 'results.csv' -exec python concat_csv.py results.csv {} +
""",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"target",
type=str,
help="The target file that will contain the concatenated csv files.",
)
parser.add_argument("sources", nargs="*", help="the csv files to be concatenated.")
args = parser.parse_args()
print(f"concatenating following files to `{args.target}`:\n{args.sources}")
(
pd.concat([pd.read_csv(file) for file in args.sources], ignore_index=True)
.drop_duplicates()
.to_csv(args.target, index=False)
)