def calculate_percentage_ranges(changes, ranges):
"""
Calculate the percentage of values that fall within each specified range.
:param changes: A list of numeric change values.
:param ranges: A list of tuples, each representing a range (inclusive).
:return: A list of percentages corresponding to each range.
"""
counts = [0] * len(ranges)
for change in changes:
for i, (lower, upper) in enumerate(ranges):
if lower <= change <= upper:
counts[i] += 1
break
total = len(changes)
percentages = [(count / total) * 100 for count in counts]
return percentages
# Example low_changes and high_changes
low_changes = [-16, -12, -7, -4, 0, 5, 12]
high_changes = [-7, -2, 0, 2, 4, 7, 10, 13, 17]
# Define ranges for Low_change
low_ranges = [(10, float('inf')), (0, 10), (0, -3), (-3, -6), (-6, -9), (-9, -12), (-12, -15), (-15, float('-inf'))]
# Calculate and print percentages for Low_change
low_percentages = calculate_percentage_ranges(low_changes, low_ranges)
print("Low_change Percentages:")
for (low, high), percentage in zip(low_ranges, low_percentages):
print(f"{low} to {high}: {percentage:.2f}%")
# Define ranges for High_change
high_ranges = [(-float('inf'), -6), (-6, -3), (-3, 0), (0, 3), (3, 6), (6, 9), (9, 12), (12, 15), (15, float('inf'))]
# Calculate and print percentages for High_change
high_percentages = calculate_percentage_ranges(high_changes, high_ranges)
print("\nHigh_change Percentages:")
for (low, high), percentage in zip(high_ranges, high_percentages):
print(f"{low} to {high}: {percentage:.2f}%")
import pandas as pd
def calculate_percentage_ranges(changes, ranges):
counts = [0] * len(ranges)
total = len(changes)
for change in changes:
for i, (lower, upper) in enumerate(ranges):
if lower <= change <= upper:
counts[i] += 1
break
return [(count / total) * 100 for count in counts]
# Load the CSV file
df = pd.read_csv('changes.csv')
# Define the ranges
low_ranges = [(10, float('inf')), (0, 10), (0, -3), (-3, -6), (-6, -9), (-9, -12), (-12, -15), (-15, float('-inf'))]
high_ranges = [(-float('inf'), -6), (-6, -3), (-3, 0), (0, 3), (3, 6), (6, 9), (9, 12), (12, 15), (15, float('inf'))]
# Calculate the percentages for Low_change
low_changes = df['Low_change']
low_percentages = calculate_percentage_ranges(low_changes, low_ranges)
# Calculate the percentages for High_change
high_changes = df['High_change']
high_percentages = calculate_percentage_ranges(high_changes, high_ranges)
# Output the results
print("Low_change Percentages:")
for (low, high), percentage in zip(low_ranges, low_percentages):
print(f"{low} to {high}: {percentage:.2f}%")
print("\nHigh_change Percentages:")
for (low, high), percentage in zip(high_ranges, high_percentages):
print(f"{low} to {high}: {percentage:.2f}%")
Before running the script, make sure your CSV file has the columns Low_change and High_change, and it's placed in the same directory as your script (or provide the full path to pd.read_csv()). This code reads the CSV, calculates the percentage of values in each specified range for both Low_change and High_change, and prints the results.