2021年3月30日星期二

How to use for loop to extract values from list?

My code works well to compute the values but to generalize this I need to use for loop to repeat the whole process for all data points and store them in the empty matrix as well.

The script is as below:

from datetime import datetime    from datetime import timedelta     names = ["year","month","day","hour","minute","second"]  df1 = pd.read_csv('T.txt',sep='\t',names=names)  df2 = pd.read_csv('Axil.txt',sep='\t',names=names)  t_series1=pd.to_datetime(df1[names]) # Time series 1  t_series2=pd.to_datetime(df2[names]) # time series 2    N = 73 # lengt of data  C =1 # number of columns  rand_hr = np.zeros(shape = (73,1)) # empty array  for u,j in zip([1],range(C)):      for i in range(1, 73, 1):          rand_hr[i,j]=i  df_hr = pd.DataFrame(rand_hr)  events=[]  for i in range(N-1):      a = df_hr[0][i]      b = df_hr[0][i+1]      t1=t_series1[0]      t2=t1+timedelta(hours=a)      t3=t1+ timedelta(hours=b)      event=((t_series2>=t2)&(t_series2<=t3)).sum()      events.append(event)  print(events)  

The for loop should work to pick all the values of t_series1 for the script

t1=t_series1[0]

and then store all the outputs in empty matrix events. In the end, the empty matrix should have 164 columns and 72 rows.

https://stackoverflow.com/questions/66880691/how-to-use-for-loop-to-extract-values-from-list March 31, 2021 at 10:05AM

没有评论:

发表评论