python 第三章降水强度————判别雨雪
首先将日期列合并
#!usr/bin/env python # -*- coding:utf-8 -*- """ @author: Suyue @file: datecon.py @time: 2025/03/12 @desc:日期合并 """ import pandas as pd # 读取CSV文件 csv_file = 'F:/地面日值数据.csv' # 替换为你的CSV文件路径 df = pd.read_csv(csv_file) # 假设CSV文件中有 '年'、'月'、'日' 三列 # 将 '年'、'月'、'日' 列转换为字符串,并补齐两位数的月份和日期 df['time'] = df['Year'].astype(str) + df['Mon'].astype(str).str.zfill(2) + df['Day'].astype(str).str.zfill(2) # 将结果写回到CSV文件中(可以覆盖原文件或写入新文件) df.to_csv(csv_file, index=False) # 打印结果 print(df['time'])
保留空格前面的值,要不然地面天气现象的记录结果太多,后面的没啥用

#!usr/bin/env python # -*- coding:utf-8 -*- """ @author: Suyue @file: datecon.py @time: 2025/03/12 @desc:保留空格前的数值 """ import pandas as pd # 读取CSV文件 csv_file = 'F:/地面日值数据.csv' # 替换为你的CSV文件路径 df = pd.read_csv(csv_file) # 保留 "WEP_Record" 列中第一个空格前面的数值 df['WEP_Record'] = df['WEP_Record'].str.split(' ').str[0] # 将结果写回到CSV文件中(可以覆盖原文件或写入新文件) df.to_csv(csv_file, index=False) # 打印结果 print(df)
把左括号删了
#!usr/bin/env python # -*- coding:utf-8 -*- """ @author: Suyue @file: datecon.py @time: 2025/03/12 @desc:去除左括号 """ import pandas as pd # 读取CSV文件 df = pd.read_csv('F:/地面日值数据.csv') # 定义一个函数来去除括号(这里假设只需要去除左括号`(`) def remove_parentheses(s): if pd.isnull(s): # 检查是否为空值 return s return s.replace('(', '') # 去除左括号`(`,如果需要去除右括号`)`,可以添加.replace(')', '') # 应用函数到"WEP_Record"列 df['WEP_Record'] = df['WEP_Record'].apply(remove_parentheses) # 将结果写回原文件(注意:这会覆盖原文件) df.to_csv('F:/地面日值数据2.csv', index=False)
根据 'WEP_Record' 列的值创建一个新的 'weather' 列,并根据规则判断天气现象,如果数值为50、60、80为rain,如果数值为70,85为snow,如果数值为68,83为sleet,其它值判断为else
#!usr/bin/env python # -*- coding:utf-8 -*- """ @author: Suyue @file: datecon.py @time: 2025/03/12 @desc:判断天气现象 """ import pandas as pd # 读取CSV文件 csv_file = 'F:/地面日值数据.csv' # 替换为你的CSV文件路径 df = pd.read_csv(csv_file) # 定义判断天气现象的函数,增加错误处理 def get_weather(value_str): try: # 尝试将字符串转换为整数,这里假设值应该是两位数,如果是单位数则前面补0(如'5'应为'05') # 但由于我们已经从CSV中读取了数据,这里直接转换即可,不需要再补0 value = int(value_str) # 如果value_str不是有效的整数表示,这里会抛出ValueError if value in [50, 60, 80]: return 'rain' elif value in [70, 85]: return 'snow' elif value in [68, 83]: return 'sleet' else: return 'else' except ValueError: # 如果转换失败,则捕获异常并返回'else' return 'else' # 注意:这里我们不再需要先将'WEP_Record'列转换为整数,因为我们在get_weather函数内部处理转换 # 创建 'weather' 列,直接应用get_weather函数到'WEP_Record'列的每个值上 df['weather'] = df['WEP_Record'].apply(get_weather) # 将结果写回到CSV文件中(可以覆盖原文件或写入新文件) # 注意:覆盖原文件可能会丢失原始数据,请谨慎操作 df.to_csv(csv_file, index=False) # 打印结果 print(df)
筛选下雨文件,根据excel中con列的关键值
#!usr/bin/env python # -*- coding:utf-8 -*- """ @author: Suyue @file: datecon.py @time: 2025/03/12 @desc:根据excel中con列筛选文件夹内txt下雨文件 """ import os import shutil import pandas as pd # 读取CSV文件 excel_file = 'F:/雨天.xlsx' # 替换为你的CSV文件路径 df = pd.read_excel(excel_file) # 假设CSV文件中有一列名为 'con',包含要查找的关键词 keywords = df['con'].tolist() # 指定包含TXT文件的目录 txt_directory = 'F:/行列值/' # 替换为你的TXT文件目录路径 # 指定目标文件夹(用于存放匹配的文件) target_directory = 'F:/rain_N/' # 替换为你的目标文件夹路径 # 如果目标文件夹不存在,则创建它 if not os.path.exists(target_directory): os.makedirs(target_directory) # 获取目录中的所有TXT文件 txt_files = [f for f in os.listdir(txt_directory) if f.endswith('.txt')] # 查找匹配的TXT文件名并复制到目标文件夹 matched_files = [] for keyword in keywords: for txt_file in txt_files: if keyword in txt_file: # 构建源文件路径和目标文件路径 source_path = os.path.join(txt_directory, txt_file) target_path = os.path.join(target_directory, txt_file) # 复制文件 shutil.copy(source_path, target_path) matched_files.append(txt_file) # # 输出匹配的文件名 # print("匹配的TXT文件名:") # for file in matched_files: # print(file) print(f"已将匹配的文件复制到目标文件夹:{target_directory}")

浙公网安备 33010602011771号