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

技术任务:基于climate_data_Dec2017.csv统计12月26日各风向读数数量

Solution: Count Wind Direction Readings for December 26th from CSV

Let's walk through how to solve this problem step by step using Python—we'll cover both a pandas-powered approach (for simplicity) and a built-in module method (no external dependencies) to fit different preferences.

Pandas makes filtering and aggregating CSV data straightforward. Here's how to do it:

Import Libraries

First, grab the tools we need:

import pandas as pd
from datetime import datetime

Load and Prepare the Data

We'll read the CSV and ensure the date column is parsed as a datetime object (critical for accurate filtering):

# Replace 'date' with your actual date column name in the CSV
df = pd.read_csv('climate_data_Dec2017.csv', parse_dates=['date'])

Tip: If your date column uses a non-standard format (like dd/mm/yyyy), add dayfirst=True to the read_csv call to avoid parsing errors.

Filter for December 26th, 2017

Narrow down the dataset to only records from our target date:

dec_26_records = df[df['date'].dt.date == datetime(2017, 12, 26).date()]

Count Wind Direction Readings

Aggregate the counts for each wind direction and convert to a dictionary:

# Replace 'wind_direction' with your actual wind direction column name
wind_dir_counts = dec_26_records['wind_direction'].value_counts().to_dict()

Handle Missing Wind Directions (Optional)

If you need to include wind directions that have no records (setting their count to 0), define all possible directions first and fill in gaps:

# Update this list to match all possible wind directions in your dataset
all_possible_dirs = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'Calm']

# Create a complete dictionary with 0 for missing entries
complete_counts = {dir: wind_dir_counts.get(dir, 0) for dir in all_possible_dirs}

Step 2: Without Pandas (Built-in CSV Module)

If you prefer to avoid external libraries, here's a vanilla Python solution:

import csv
from datetime import datetime
from collections import defaultdict

# Configure column indexes (update these to match your CSV structure)
DATE_COL_INDEX = 0
WIND_DIR_COL_INDEX = 2
all_possible_dirs = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW', 'Calm']

# Initialize a counter for wind directions
wind_counts = defaultdict(int)

# Process the CSV line by line
with open('climate_data_Dec2017.csv', 'r') as csv_file:
    reader = csv.reader(csv_file)
    next(reader)  # Skip the header row
    
    for row in reader:
        # Parse the date from the row
        record_date = datetime.strptime(row[DATE_COL_INDEX], '%Y-%m-%d').date()
        if record_date == datetime(2017, 12, 26).date():
            wind_dir = row[WIND_DIR_COL_INDEX]
            wind_counts[wind_dir] += 1

# Fill in missing directions with 0
complete_counts = {dir: wind_counts.get(dir, 0) for dir in all_possible_dirs}

Final Notes

  • Make sure to update column names/indexes and the list of possible wind directions to match your actual CSV file structure.
  • Both approaches will output a dictionary where keys are wind directions and values are the number of readings on December 26th.

内容的提问来源于stack exchange,提问作者D.Ronald

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:14:20