遍历Pandas数据框并根据条件复制到新的数据框

时间:2020-07-31 16:41:24

标签: python pandas loops dataframe conditional-statements

我有一个数据框df,其中有6000余行数据,其日期时间索引格式为YYYY-MM-DD,列IDwater_levelchange

我要:

  1. 浏览change列中的每个值并确定转折点
  2. 找到转折点时,请将整行数据(包括索引)复制到新的数据框中,例如turningpoints_df
  3. 对于循环中标识的每个新转折点,将那一行数据添加到我的新数据帧turningpoints_df中,以便得到这样的结果:
               ID    water_level    change
date           
2000-10-01      2         5.5        -0.01
2000-12-13     40        10.0         0.02
2001-02-10    150         1.1       -0.005
2001-07-29    201        12.4         0.01
...           ...         ...          ...

我当时正在考虑采用定位方法,例如(纯粹是说明性的):

turningpoints_df = pd.DataFrame(columns = ['ID', 'water_level', 'change'])

for i in range(len(df['change'])):
    if [i-1] < 0 and [i+1] > 0:
        #this is a min point and take this row and copy to turningpoints_df
    elif [i-1] > 0 and [i+1] < 0:
        #this is a max point and take this row and copy to turningpoints_df
    else: 
        pass 

我的问题是,我不确定如何将change列中的每个值与之前和之后的值进行比较,然后再在条件满足时如何将数据行提取到新的df中被满足。

2 个答案:

答案 0 :(得分:0)

听起来您想使用DataFrame的shift方法。

#  shift values down by 1:

df[change_down] = df[change].shift(1)


#  shift values up by 1:
df[change_up] = df[change].shift(-1)

然后您应该能够比较每一行的值,并继续进行您要达到的目标。

for row in df.iterrows():
   *check conditions here*

答案 1 :(得分:0)

使用一些NumPy功能,使您可以roll()向前或向后进行一系列操作。然后将 prev next 放在同一行上,这样就可以使用一个简单的函数来apply(),因为一切都在同一行上。

from decimal import *
import numpy as np
d = list(pd.date_range(dt.datetime(2000,1,1), dt.datetime(2010,12,31)))
df = pd.DataFrame({"date":d, "ID":[random.randint(1,200) for x in d], 
     "water_level":[round(Decimal(random.uniform(1,13)),2) for x in d], 
      "change":[round(Decimal(random.uniform(-0.05, 0.05)),3) for x in d]})

# have ref to prev and next, just apply logic
def turningpoint(r):
    r["turningpoint"] = (r["prev_change"] < 0 and r["next_change"] > 0) or \
        (r["prev_change"] > 0 and r["next_change"] < 0)
    return r

# use numpy to shift "change" so have prev and next on same row as new columns
# initially default turningpoint boolean
df = df.assign(prev_change=np.roll(df["change"],1), 
          next_change=np.roll(df["change"],-1),
          turningpoint=False).apply(turningpoint, axis=1).drop(["prev_change", "next_change"], axis=1)
# first and last rows cannot be turning points
df.loc[0:0,"turningpoint"] = False
df.loc[df.index[-1], "turningpoint"] = False

# take a copy of all rows that are turningpoints into new df with index
df_turningpoint = df[df["turningpoint"]].copy()
df_turningpoint