如何将最后一个值替换为kWh(单位:kWh)

2024-05-15 03:30:12 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我有一个熊猫的代码(我想在这里把瓦特转换成千瓦时):

new = df['datetime;W'].str.split('(;| )',n=2, expand=True)
df['Date_Time']= new[0]
df['Watts']= new[2]
df['Date_Time'] = pd.to_datetime(df['Date_Time'], format='%Y%m%d:%H%M', errors='ignore')
df['Watts'] = df['Watts'].str.replace(',', '.').astype(float)
kiloWatt = df.select_dtypes(exclude=['object','datetime']) * 0.001
df['kWatt'] = kiloWatt['Watts']
df['kWh']= 'NaN'
df.drop(columns =['datetime;W', 'Watts'], inplace = True)
   for index, row in df.iterrows():
     if index < len(df)-1:
       df.loc[index,'kWh'] = ((df['kWatt'][index] + df['kWatt'][index+1]) / 2)  *((df['Date_Time'][index+1]-df['Date_Time'][index]).seconds / 3600)
       df['kWh'] = df['kWh'].astype(float)
df.drop(columns =['kWatt'], inplace = True)'''

    date_time        kWh
2016-12-31 21:55:00  0.0
2016-12-31 22:55:00  0.0
2016-12-31 23:55:00  NaN

我想在我的最后一行也能得到价值,但我不知道如何!!!??? 如果可能的话,我也想改进我的代码(在30分钟而不是1小时内获取col:date\u time)。谢谢


Tags: 代码truedfnewdatetimedateindextime

热门问题