有个雨滴谱的数据,情况是有雨滴的时候会记录那个时刻的雨滴情况,但是无雨滴的时间没有记录
那么我想花一个雨滴时间序列的情况,就需要补全没有雨滴的时间,并且记录为0
数据情况如下:
python代码:
#!usr/bin/env python # -*- coding:utf-8 -*- """ @author: Su @file: timecomplet.py @time: 2024/03/18 @desc: """ import pandas as pd input_file = '/呼和浩特新城区.xlsx' output_file = '/呼和浩特新城区_second.xlsx' df = pd.read_excel(input_file) df['观测时间'] = pd.to_datetime(df['观测时间'],format='%Y-%m-%d %H:%M') df.set_index('观测时间',inplace=True) start_date = pd.to_datetime('2023-06-27 22:44',format='%Y-%m-%d %H:%M') end_date = pd.to_datetime('2023-06-28 1:51', format='%Y-%m-%d %H:%M') date_range = pd.date_range(start=start_date,end=end_date,freq='T') df_filled = df.reindex(date_range,fill_value=0) df_filled.reset_index(inplace=True) df_filled['观测时间'] = df_filled['index'].dt.strftime('%Y-%m-%d %H:%M') df_filled.drop(df_filled.columns[3],axis=1,inplace=True) cols = list(df_filled.columns) cols = [cols[-1]] + cols[:-1] df_filled = df_filled[cols] df_filled.to_excel('/呼和浩特新城区_second.xlsx',index=False)
标签:-%,python,雨滴,df,pd,date,补零,缺失,filled From: https://www.cnblogs.com/shirleysu90/p/18081024