簡體   English   中英

通過遍歷熊貓中的連續行來創建新列

[英]Create a new column based by Iterating over consecutive rows in pandas

我有一個數據框:

import pandas as pd
import numpy as np
df = pd.DataFrame({'a':[1,20,3,4,50,6],
               'b':[12,43,78,23,14,28],
               'c': [100,200,300,400,500,600]})`

我想遍歷連續的行,這樣,

如果下一行的'a'-當前行的'a'小於10

然后檢查下一行的'c'-當前行的'b' 小於400

return 0

else return Nan.

我想使用.apply編寫代碼。

def query(row,df):
    try:
        i = row.name
        curr = df.iloc[i]
        curr_a = curr['a']
        next = df.iloc[i+1]
       next_a = next['a']
        if (next_a-curr_a) < 10:
            print(next_a,curr_a)
            curr_b = curr['b']
            next_c = next['c']
            print(next_c,curr_b)
           if (next_c - curr_b) < 400:
                return 0
        else:
            diff = np.nan
        return diff
    except:
        pass

df['new_col'] = df.apply(lambda x: query(x,df),axis=1)

基本上,我獲取當前行的索引,即i ,並將其傳遞給一個函數,在其中使用df.iloc[i]定位當前行,然后使用df.iloc[i+1]定位下一行,然后檢查條件。 但我認為這不是最好的方法。

有一個更好的方法嗎? 可能使用.shift或任何.shift方式? 任何線索都將有所幫助。

shift使用np.where

np.where(((df.a.shift(-1)-df.a)<10)&((df.c.shift(-1)-df.b)<400),0,np.NaN)
Out[85]: array([nan,  0.,  0., nan, nan, nan])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM