-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfieldinsertion.py
More file actions
72 lines (53 loc) · 2.16 KB
/
Copy pathfieldinsertion.py
File metadata and controls
72 lines (53 loc) · 2.16 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
CSV field correction utility.
This script updates CSV files by inserting 'Unknown' species labels when
species fields are empty but corresponding count fields indicate animal presence.
It is designed as a command-line utility for cleaning legacy or inconsistently
formatted datasets prior to ingestion into the main analysis pipeline.
"""
import pandas as pd
import sys
def update_csv(file, output_file=None):
"""Fills blank species fields with 'Unknown' when count data exists.
For rows where the species column (column F/index 5) is empty but the
count column (column I/index 8) has a value greater than 0, inserts
'Unknown' as the species name.
Args:
file (str): Path to the input CSV file.
output_file (str, optional): Path for the output CSV. Defaults to
overwriting the input file.
"""
try:
df = pd.read_csv(file)
if len(df.columns) < 9:
print("Error: Not enough columns")
return
col_f = df.columns[5]
col_i = df.columns[8]
# Apply the rule: if column F is empty and column I > 0, set column F to "Unknown"
mask = (df[col_f].isna() | (df[col_f] == '')) & (pd.to_numeric(df[col_i], errors='coerce') > 0)
df.loc[mask, col_f] = 'Unknown'
# set output
if output_file is None:
output_file = file
df.to_csv(output_file, index=False)
print("Success!")
# Print summary of changes
changes_made = mask.sum()
print(f"Changes made: {changes_made} rows updated")
except FileNotFoundError:
print(f"Error: File '{file}' not found.")
except Exception as e:
print(f"Error processing file: {str(e)}")
def main():
"""Main function to handle command line arguments"""
if len(sys.argv) < 2:
print("Usage: python script.py <input_csv_file> [output_csv_file]")
print("Example: python script.py data.csv")
print("Example: python script.py data.csv updated_data.csv")
return
input_file = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) > 2 else None
update_csv(input_file, output_file)
if __name__ == "__main__":
main()