【问题标题】:For and if in dataframe column - add to another dataframe some rows beforeFor and if in dataframe column - 添加到另一个数据框之前的一些行
【发布时间】:2020-02-10 16:14:06
【问题描述】:

我尝试遍历我的数据并检查一列(雨)是否大于 0,如果为真,我需要在 i 索引之前取 100 行,如果 i 小于 100,我忽略它并继续运行,并将它们添加到另一个数据框中。 我的代码:

import pandas as pd

data = pd.read_csv('weather_forecast.csv')
data_before_rain = pd.DataFrame()
for index,row in data.iterrows():
    if row['rain'] > 1:
        data_before_rain.append(data.iloc[(index-100):index])

print(data_before_rain)

DataFrame 数据示例:

                  time  ghi  dni  ...  barometric_pressure  rain  sensor_cleaning
0     01/07/2018 07:14   34    0  ...                981.8   0.1                0
1     01/07/2018 07:15   34    0  ...                981.9   0.0                0
2     01/07/2018 07:16   35    0  ...                981.9   0.0                0
3     01/07/2018 07:17   36    0  ...                981.9   0.0                0
4     01/07/2018 07:18   37    0  ...                981.9   0.1                0
5     01/07/2018 07:19   38    0  ...                982.0   0.0                0
6     01/07/2018 07:20   39    0  ...                982.0   0.0                0
7     01/07/2018 07:21   40    0  ...                982.0   0.0                0
8     01/07/2018 07:22   42    0  ...                982.0   0.0                0
9     01/07/2018 07:23   43    0  ...                982.0   0.0                0
10    01/07/2018 07:24   44    0  ...                982.0   0.0                0
11    01/07/2018 07:25   45    0  ...                982.0   0.1                0
12    01/07/2018 07:26   46    0  ...                982.1   0.0                0

当我尝试使用 = (data_before_rain = data.iloc[index-100:index]) 代替 append() 方法时,它仅适用于最后 100 行。 当我尝试append() 方法时,输出是:

Empty DataFrame
Columns: []
Index: []

我该怎么做?

【问题讨论】:

  • 检查for i in data: 实际在做什么(试试for i in data: print(i))。 (提示,数据帧有自己的迭代方法,df.iterrows()
  • @Ram Rahamim if rain > 1 then get last 100 rows and put to the new df, if there are no 100 rows before rain > 1 pass and continue search?
  • @ZarakiKenpachi 是的。

标签: python pandas dataframe for-loop if-statement


【解决方案1】:

对于如下格式的数据:

import pandas as pd
import numpy as np

random_data = np.random.uniform(0, 1, 49)
random_data = np.append(random_data, 1.1)
random_data = np.append(random_data, np.random.uniform(0, 1, 59))
random_data = np.append(random_data, 1.1)
random_data = np.append(random_data, np.random.uniform(0, 1, 139))
random_data = np.append(random_data, 1.1)
random_data = np.append(random_data, np.random.uniform(0, 1, 20))

df = pd.DataFrame({'data':np.linspace(1,150,150), 'rain':random_data})

查找rain > 1的行,找到大于100的适当索引,最后从找到的索引中获取最后100行。

fit_list = df.index[df['rain'] > 1].to_list()
proper_index_list = [x for x in fit_list if x > 100]
df_list = []
for index in proper_index_list:
    out = df.iloc[index-100: index]
    df_list.append(out)

df = pd.concat(df_list)

输出:

           data      rain
9      5.985130  0.105051
..          ...       ...
244  136.152416  0.968460
248  138.368030  0.989770

df_shape = (200, 2)

【讨论】:

  • 我编辑了我的问题。雨有时出现大于 1,我需要一个数据框中的所有行。
  • @RamRahamim 您在使用rain > 1 进行索引之前获得了最后 100 行。不明白你上次评论的意思。
  • 我不仅需要最后 100 行。当雨大于 1 时,我需要添加到数据帧 100 行。例如,如果在第 3677 行和第 6832 行雨大于 1,则新数据帧需要包含第 3577-3676 和 6732-6731 行
  • @RamRahamim 何时每次出现rain >1 获取最后100行并将它们添加到新的df?最后的评论是指范围:3577-3676 和 6732-6831?
  • 是的:)。每次到data_before_rain。是的,有范围。
猜你喜欢
  • 2022-12-28
  • 2015-12-11
  • 2015-10-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2019-05-24
  • 2021-12-13
相关资源
最近更新 更多