條件迭代我有一個數據幀df
看起來像:在數據幀
id location grain
0 BBG.XETR.AD.S XETR 16.545
1 BBG.XLON.VB.S XLON 6.2154
2 BBG.XLON.HF.S XLON NaN
3 BBG.XLON.RE.S XLON NaN
4 BBG.XLON.LL.S XLON NaN
5 BBG.XLON.AN.S XLON 3.215
6 BBG.XLON.TR.S XLON NaN
7 BBG.XLON.VO.S XLON NaN
在現實中這個數據幀會大很多。我想迭代這個數據幀返回'grain'
值,但我只對'grain'列中有值(不是NaN)的行感興趣。因此,只有回國,因爲我遍歷數據框以下值:
16.545
6.2154
3.215
我可以遍歷使用數據框:
for staticidx, row in df.iterrows():
value= row['grain']
但這返回包括那些NaN值的所有行的值。是否有辦法從數據框中刪除NaN行或跳過數據框中的grain等於NaN的行?
非常感謝
'df.grain [〜pd.isnull(df.grain)]'? – Psidom
或者你可以這樣做:'df.ix [df.grain.notnull(),'grain']' – MaxU