A more efficient code

Joined
Apr 11, 2022
Messages
2
Reaction score
0
I just started with Python. I want to add the information of the array ‘toc’ to the array ‘toc_n.’ The first two columns of both ones are ‘year’ and ‘day of the year,’ but the array ‘toc’ lacks some days (i.e., fewer rows). I wrote the following code. Could you please suggest something more efficient/clever?

for i in range(2196):
for j in range(2173):
dd = np.logical_and(toc_n[i,0] == toc[j,0] , toc_n[i,1] == toc[j,1])
if dd == True:
toc_n[i,2]=toc[j,2]
 
Joined
Apr 11, 2022
Messages
2
Reaction score
0
In terms of computation time, it looks that "pandas.merge" does the job much faster

df_toc_n = pd.DataFrame(toc_n, columns = ['y','doy','val'])
df_toc = pd.DataFrame(toc, columns = ['y','doy','val'])
new_toc = pd.merge(df_toc_n, df_toc, on=['y', 'doy'])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top