在尝试使用烧瓶My_previous_question多次失败之后,我决定换个方向使用破折号。
仅供参考-这是我第一次破折号,对于造成任何混乱,我们深表歉意。
我能够在线进行一些搜索,并且能够找到this_post,其中几乎包含了我的API所需的一切。 我将示例中的函数“ some_function”替换为我的函数:
def transform(df):
#count the number of columns in the data frame
col=len(df.columns)
if col>3:
#Transform a matrix to a vector
df=df.set_index(df.columns[0]).stack().reset_index()
df[['Date','level_1']]=df[['Date','level_1']].astype(str)
df['dtime']=df['Date']+' '+df['level_1']
df['dtime'] = pd.to_datetime(df['dtime'])
df=df.drop(['Date','level_1'],axis=1)
df.columns=['KW','dtime']
df=df[['dtime','KW']]
df=df.sort_values(by='dtime',ascending=False)
df.reset_index(inplace=True,drop=True)
#df.index = pd.to_datetime(df[df.columns[[0,1]]].astype(str).apply('-'.join,1))
else:
df.columns =(['dtime','kW'])
df['dtime'] = pd.to_datetime(df['dtime'])
df['dtime'] = pd.to_datetime(df['dtime'])
#find the interval by substracting the second date from the first one
a = df.loc[0,'dtime']
b = df.loc[1,'dtime']
c = a - b
minutes = c.total_seconds() / 60
d=int(minutes) #d can be only 15,30 or 60
#This function will create new row to the time series anytime when it finds gaps and will fill it with NaN or leave it blank.
#df.drop_duplicates(keep='first') keeps the first value of duplicates
if d==15:
df.drop_duplicates(keep='first',inplace=True)
df= df.set_index('dtime').asfreq('-15T')
elif d==30:
df.drop_duplicates(keep='first',inplace=True)
df= df.set_index('dtime').asfreq('-30T')
elif d==60:
df.drop_duplicates(keep='first',inplace=True)
df= df.set_index('dtime').asfreq('-60T')
else:
None
return df
脚本运行正常,没有任何错误,但是输出是奇怪的output。 我不确定自己在做什么错。
此外:
1。如何以excel格式导出文件
2.(来自示例)如果导入excel文件,函数(some_function)是否会遍历所有工作表?我问这个问题是因为我需要知道是否必须为我的函数创建一个迭代(转换)