SpringClean.py
· 2.7 KiB · Python
Raw
import pandas as pd
import glob
import os
# ---------------------------------------------------------
# STEP 1 - Ask user which spring to process
# ---------------------------------------------------------
spring_name = input("Enter spring number (example: Spring_1040_01): ").strip()
# ---------------------------------------------------------
# STEP 2 - Find all matching test files
# ---------------------------------------------------------
file_pattern = f"{spring_name}_Test_*.csv"
file_list = glob.glob(file_pattern)
# Make sure files are processed in order
file_list.sort()
if len(file_list) == 0:
print(f"No files found matching: {file_pattern}")
exit()
# ---------------------------------------------------------
# STEP 3 - Variables used while combining files
# ---------------------------------------------------------
combined_data = []
time_offset = 0
# ---------------------------------------------------------
# STEP 4 - Process each test file
# ---------------------------------------------------------
for file in file_list:
print(f"Processing: {file}")
# Read CSV
df = pd.read_csv(file)
# -----------------------------------------------------
# Remove rows containing empty data
# -----------------------------------------------------
df = df.dropna()
# -----------------------------------------------------
# Find the time column
#
# Change this name if your machine uses a different
# column title.
# -----------------------------------------------------
time_column = "Time (s)"
# -----------------------------------------------------
# Create continuous time column
# -----------------------------------------------------
df["Continuous Time (s)"] = df[time_column] + time_offset
# -----------------------------------------------------
# Determine ending time for next test
# -----------------------------------------------------
last_time = df[time_column].iloc[-1]
time_offset += last_time
# Store cleaned data
combined_data.append(df)
# ---------------------------------------------------------
# STEP 5 - Combine all tests into one dataframe
# ---------------------------------------------------------
final_df = pd.concat(combined_data, ignore_index=True)
# ---------------------------------------------------------
# STEP 6 - Create output filename
# ---------------------------------------------------------
output_file = f"{spring_name}_Cleaned.csv"
# ---------------------------------------------------------
# STEP 7 - Save cleaned data
# ---------------------------------------------------------
final_df.to_csv(output_file, index=False)
print()
print(f"Finished!")
print(f"Output saved as: {output_file}")
| 1 | import pandas as pd |
| 2 | import glob |
| 3 | import os |
| 4 | |
| 5 | |
| 6 | # --------------------------------------------------------- |
| 7 | # STEP 1 - Ask user which spring to process |
| 8 | # --------------------------------------------------------- |
| 9 | spring_name = input("Enter spring number (example: Spring_1040_01): ").strip() |
| 10 | |
| 11 | |
| 12 | # --------------------------------------------------------- |
| 13 | # STEP 2 - Find all matching test files |
| 14 | # --------------------------------------------------------- |
| 15 | file_pattern = f"{spring_name}_Test_*.csv" |
| 16 | |
| 17 | file_list = glob.glob(file_pattern) |
| 18 | |
| 19 | # Make sure files are processed in order |
| 20 | file_list.sort() |
| 21 | |
| 22 | if len(file_list) == 0: |
| 23 | print(f"No files found matching: {file_pattern}") |
| 24 | exit() |
| 25 | |
| 26 | |
| 27 | # --------------------------------------------------------- |
| 28 | # STEP 3 - Variables used while combining files |
| 29 | # --------------------------------------------------------- |
| 30 | combined_data = [] |
| 31 | |
| 32 | time_offset = 0 |
| 33 | |
| 34 | |
| 35 | # --------------------------------------------------------- |
| 36 | # STEP 4 - Process each test file |
| 37 | # --------------------------------------------------------- |
| 38 | for file in file_list: |
| 39 | |
| 40 | print(f"Processing: {file}") |
| 41 | |
| 42 | # Read CSV |
| 43 | df = pd.read_csv(file) |
| 44 | |
| 45 | # ----------------------------------------------------- |
| 46 | # Remove rows containing empty data |
| 47 | # ----------------------------------------------------- |
| 48 | df = df.dropna() |
| 49 | |
| 50 | # ----------------------------------------------------- |
| 51 | # Find the time column |
| 52 | # |
| 53 | # Change this name if your machine uses a different |
| 54 | # column title. |
| 55 | # ----------------------------------------------------- |
| 56 | time_column = "Time (s)" |
| 57 | |
| 58 | # ----------------------------------------------------- |
| 59 | # Create continuous time column |
| 60 | # ----------------------------------------------------- |
| 61 | df["Continuous Time (s)"] = df[time_column] + time_offset |
| 62 | |
| 63 | # ----------------------------------------------------- |
| 64 | # Determine ending time for next test |
| 65 | # ----------------------------------------------------- |
| 66 | last_time = df[time_column].iloc[-1] |
| 67 | |
| 68 | time_offset += last_time |
| 69 | |
| 70 | # Store cleaned data |
| 71 | combined_data.append(df) |
| 72 | |
| 73 | |
| 74 | # --------------------------------------------------------- |
| 75 | # STEP 5 - Combine all tests into one dataframe |
| 76 | # --------------------------------------------------------- |
| 77 | final_df = pd.concat(combined_data, ignore_index=True) |
| 78 | |
| 79 | |
| 80 | # --------------------------------------------------------- |
| 81 | # STEP 6 - Create output filename |
| 82 | # --------------------------------------------------------- |
| 83 | output_file = f"{spring_name}_Cleaned.csv" |
| 84 | |
| 85 | |
| 86 | # --------------------------------------------------------- |
| 87 | # STEP 7 - Save cleaned data |
| 88 | # --------------------------------------------------------- |
| 89 | final_df.to_csv(output_file, index=False) |
| 90 | |
| 91 | print() |
| 92 | print(f"Finished!") |
| 93 | print(f"Output saved as: {output_file}") |