我有一个pandas df,它的一列具有正或负浮点值:

snapshot                        0 (column name)
2018-06-21 00:00:00        -60.18
2018-06-21 00:00:15         43.78
2018-06-21 00:00:30        -22.08


现在,我想将正值附加到名为以下内​​容的列表:

excessSupply=[]


负值则为:

excessLoad=[]


通过

        for row in self.dfenergyBalance:

        if self.dfenergyBalance['0'] < 0:

            self.excessLoad.append(self.dfenergyBalance['0'])

        else:

            self.excessLoad.append(0)


(对于过量供应是if条件self.dfenergyBalance> 0)

结果是列名“ 0”的关键错误

最佳答案

我认为没有循环(慢)是必要的,而且列名似乎是数字0

mask = dfenergyBalance[0] < 0

excessSupply = dfenergyBalance.loc[mask, 0].tolist()
excessLoad = dfenergyBalance.loc[~mask, 0].tolist()

print (excessSupply)
[-60.18, -22.08]

print (excessLoad)
[43.78]


编辑:

对于仅0为正值长度的列表:

excessLoad = [0] * (~mask).sum()
print (excessLoad)
[0]


如果只需要一个列表,将正值替换为0的正值:

L = np.where(mask, dfenergyBalance[0], 0).tolist()
print (L)
[-60.18, 0.0, -22.08]

10-06 11:26