0

I am trying to write a code that would get rid of speed data above water level. So far I have 9 bins (each 25 cm) and speed is measured for each of them but I need to compare the measured water level that I have with the bin height to make sure it is not using the above water level data. so far I have made a list of the bins:


#The sample dataframe looks like this :

df=pd.DataFrame([[1.5, 0.2, 0.3, 0.33], [1.3, 0.25, 0.31, 0.35], [1.4, 0.21, 0.32, 0.36]], columns=['pressure', 'bin1', 'bin2', 'bin3'])

df2= pd.DataFrame ([1.25, 1.35, 1.55], columns=['bin heights'])

#to make things easier I defined separate lists 

y1 = df['pressure'][:] #shows water level

s1 = df['bin1'][:] #shows speed for bin 1

s2= df['bin2'][:] #shows speed for bin 2

s3= df['bin3'][:] #shows speed for bin 3



#cleaning up data above water level; gives me the right index

diff1=np.subtract(y1, df2['bin heights'][0])
p1=diff1[(diff1<= 0.05) & (0<diff1)].index

diff2=np.subtract(y1, df2['bin heights'][1])
p2=diff2[(diff2<= 0.05) & (0<diff2)].index

diff3=np.subtract(y1, df2['bin heights'][2])
p3=diff3[(diff3<= 0.05) & (0<diff3)].index


        

I created the data frame below and it seems to work:

index = p1.append([p2,p3])
values=[df['bin1'][p1], df['bin2'][p2], df['bin3'][p3]]
df0 = pd.DataFrame(values)
df0=df0.sort_index()
df02 = df0.T

Now there is only one value for each row and the rest are NaN, how do I plot row by row and get that value without having to specify the column?

Found it (I defined a new column with all non NaN values):

cols =  [df02.columns.str.startswith('speed')]

df02['speed'] = df02.filter(like='speed').max(1)

print(df02)
Mo Raz
  • 3
  • 3
  • Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Currently, it is very difficult to reproduce your problem. But a general pandas rule says: "If you use loops, you're probably not doing it right." – Mr. T Jan 15 '22 at 05:53
  • @Mr.T thank you I did, I think it should look good now. In this case I think I have to use loops because I want it to constantly check y1 with the other bins and pick the right one – Mo Raz Jan 16 '22 at 20:29
  • @Mr.T I got rid of the loops and now I have the index I wanted but now I can only plot them separately and scattered so they don't produce multiple lines on top each other – Mo Raz Jan 17 '22 at 01:33
  • As before, this is not a reproducible example. What are `df1` and `df4`? No data column in this question is called `Xspeed3`. Why do you `axs[0]`? Will the data be distributed over several subplots? This is not mentioned. We only see what you write here, so you have to provide all relevant information if you expect some help. – Mr. T Jan 17 '22 at 12:14
  • @Mr.T The last line was from my original plot just as an example, I got rid of it. – Mo Raz Jan 17 '22 at 19:25

0 Answers0