list - Running through appended data frame to locate where the first positive number is in python -


i have appended series using pandas. call s. each s[i], i, has 50 data points. call these j.

i want go through each i, , instance j=1, find first positive s[i][1] occurs , record number is. output looking hence 2 dataframe [i,1] records j's each , [i,2] records positive number was.

preferably, vectorized version instance sapply/apply in r.

i hope description made sense.. hope there out there me this!

the following example i=4 , j=6.

s[0]: 2013-01-02_59   -0.004739 2013-01-02_61   +0.002435 2013-01-02_74   -0.004772 2013-01-02_75   -0.004772 2013-01-02_77   -0.002452 2013-01-02_78   -0.009423  s[1]: 2013-01-02_60   -0.007048 2013-01-02_62   -0.002435 2013-01-02_75   +0.004772 2013-01-02_76   -0.002446 2013-01-02_78   +0.007114 2013-01-02_79   -0.004772  s[2]:  2013-01-02_61   -0.004739 2013-01-02_63   +0.002435 2013-01-02_76   -0.002446 2013-01-02_77   -0.004772 2013-01-02_79   -0.002452 2013-01-02_80   +0.002446  s[3]:  2013-01-02_62   -0.004739 2013-01-02_64   +0.002435 2013-01-02_77   -0.004772 2013-01-02_78   +0.009423 2013-01-02_80   -0.000121 2013-01-02_81   -0.004772 

my desire output in example thus:

output: na    na 1     +0.002435 2     +0.004772 4     +0.009423 2     +0.007114 3     +0.002446 

the first row of output na because never positive.

the below identify index , value of first positive value per series, , insert np.nan in cases there no positive value. sample data:

df = pd.dataframe() in range(10):     df = pd.concat([df, pd.series(data=np.random.uniform(-1, 1, 50), name=i)], axis=1)  df = df.transpose()  <class 'pandas.core.frame.dataframe'> int64index: 10 entries, 0 9 data columns (total 50 columns): 0     10 non-null float64 1     10 non-null float64 2     10 non-null float64 3     10 non-null float64 4     10 non-null float64 5     10 non-null float64 .... 45    10 non-null float64 46    10 non-null float64 47    10 non-null float64 48    10 non-null float64 49    10 non-null float64 dtypes: float64(50) 

use: df.loc[3, :] = -1

tmp = df.apply(lambda x: pd.dataframe({'value': x[x > 0]}).reset_index().iloc[0] if not x[x > 0].empty else (x.index[-1], np.nan), axis=1) 

to pairs of index, values in columns each original series i, latter referenced `index:

   index     value 0      1  0.608962 1      2  0.487893 2      1  0.850135 3     49       nan 4      1  0.870091 5      2  0.469713 6      1  0.331851 7      0  0.036980 8      0  0.387298 9      3  0.723645 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -