运行附加数据框以找到python中第一个正数的位置

时间:2016-01-07 15:09:58

标签: python list pandas append

我有一个使用熊猫的附加系列。我将其称为S.对于某些我来说,每个S [i]有50个数据点。我将这些称为j。

我想通过每个i,例如j = 1,找到第一个正s [i] [1]出现的位置并记录数字是什么。我正在寻找的输出是i乘2的数据帧,其中[i,1]记录每个i的j和[i,2]记录正数是什么。

最好,我想要一个矢量化版本,例如像R。中的sapply / apply。

我希望描述有意义。我希望有人可以帮我解决这个问题!

以下是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

因此,我在这个例子中的欲望输出是:

Output:
NA    NA
1     +0.002435
2     +0.004772
4     +0.009423
2     +0.007114
3     +0.002446

输出的第一行是NA,因为它从来都不是正面的。

1 个答案:

答案 0 :(得分:0)

以下内容将标识每index的第一个正值的valueseries,并在没有正值的情况下插入np.nan。一些样本数据:

df = pd.DataFrame()
for i 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 to 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)

使用:     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)

index中为每个原​​始values columns获取seriesi对,后者由`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