You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用复杂IF-THEN语句缩减特定结构CSV的列值

CSV Column Value Reduction with Complex IF-THEN Logic

Let's walk through how to tackle this CSV transformation task step by step. I'll use Python with pandas (the standard tool for CSV data manipulation) since it makes conditional logic and column operations straightforward and readable.

Step 1: Clarify Your Conditional Rules

First, let's ground this in realistic example rules (you can tweak these to match your exact requirements):

  • Winter: If any monthly value in your defined Winter months (let's say Jan-Mar, matching your sample's 201803, 201804, etc. columns) is 10, set Winter to 10. If no 10s but at least one 4, set to 4. Otherwise, set to the lowest monthly value (which is 2 in your sample).
  • Summer: If the average of Summer months (Jun-Aug, matching your 201807, 201808, 201809 columns) is ≥5, set to 10. If average is ≥3 but <5, set to 4. Otherwise, set to 2.
  • Annual: Set to the maximum value across all 6 monthly columns.

Step 2: Full Code Implementation

Here's the code to read your CSV, apply the conditional logic, and save the processed file:

import pandas as pd

# 1. Load your CSV file
df = pd.read_csv("your_input_file.csv")

# 2. Map date columns to seasons by extracting month numbers
month_cols = [col for col in df.columns if col not in ['ID', 'Winter', 'Summer', 'Annual']]
# Convert column names (like '20180922') to datetime to get month values
month_numbers = pd.to_datetime(month_cols, format='%Y%m%d').month

# Define which months belong to each season (adjust these ranges to match your actual definition!)
winter_cols = [month_cols[i] for i, m in enumerate(month_numbers) if m in [1, 2, 3]]  # Jan-Mar
summer_cols = [month_cols[i] for i, m in enumerate(month_numbers) if m in [6, 7, 8]]  # Jun-Aug

# 3. Apply IF-THEN logic for Winter column
def compute_winter(row):
    winter_values = row[winter_cols].astype(int)
    if (winter_values == 10).any():
        return 10
    elif (winter_values == 4).any():
        return 4
    else:
        return winter_values.min()

df['Winter'] = df.apply(compute_winter, axis=1)

# 4. Apply IF-THEN logic for Summer column
def compute_summer(row):
    summer_values = row[summer_cols].astype(int)
    avg = summer_values.mean()
    if avg >= 5:
        return 10
    elif avg >= 3:
        return 4
    else:
        return 2

df['Summer'] = df.apply(compute_summer, axis=1)

# 5. Compute Annual column (max of all monthly values)
df['Annual'] = df[month_cols].astype(int).max(axis=1)

# 6. Save the processed CSV
df.to_csv("your_output_file.csv", index=False)

Step 3: Customize to Your Exact Rules

To adapt this to your specific IF-THEN requirements:

  • Adjust the month ranges in winter_cols and summer_cols to match your actual season definitions (e.g., change to [11,12,1] if Winter is Nov-Feb).
  • Rewrite the conditional checks in compute_winter and compute_summer to align with your rules (e.g., add nested conditions, change threshold values, or use sum instead of average).
  • If your monthly values aren't integers, remove the astype(int) calls or adjust to astype(float) as needed.

内容的提问来源于stack exchange,提问作者Myco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:33:42