簡體   English   中英

For and if in dataframe column - 在另一個數據框之前添加一些行

[英]For and if in dataframe column - add to another dataframe some rows before

我嘗試遍歷我的數據並檢查一列(雨)是否大於 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

當我嘗試代替append()方法= ( data_before_rain = data.iloc[index-100:index] ) 時,它僅適用於最后 100 行。 當我嘗試append()方法時,輸出是:

Empty DataFrame
Columns: []
Index: []

我怎樣才能做到這一點?

對於格式如下的數據:

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)

暫無
暫無

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

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