基于序列筛选行

2024-04-26 05:12:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在筛选列中的值时遇到了一个问题。我有一个数据帧(data),它看起来像下面的那个。你知道吗

 Index                                                            Value
2019-11-22 00:00:00                                                0.0  
2019-11-22 00:05:00                                                1.0  
2019-11-22 00:10:00                                                2.0  
2019-11-22 00:15:00                                                3.0  
2019-11-22 00:20:00                                                4.0  
2019-11-22 00:25:00                                                5.0  
2019-11-22 00:30:00                                                6.0  
2019-11-22 00:35:00                                                7.0  
2019-11-22 00:40:00                                                8.0  
2019-11-22 00:45:00                                                0.0  
2019-11-22 00:50:00                                                0.0  
2019-11-22 00:55:00                                                1.0  
2019-11-22 01:00:00                                                2.0  
2019-11-22 01:05:00                                                3.0  
2019-11-22 01:10:00                                                4.0  
2019-11-22 01:15:00                                                5.0  

我想保留大于5的一系列值,并将所有其他值赋值为零。例如,如果值在1-5之间,则5之前的所有值都应设置为零,如果有8行的值在1-8之间,则代码应将它们保持为原来的值是的。是的最终输出应如下所示。你知道吗

 Index                                                            Value
2019-11-22 00:00:00                                                0.0  
2019-11-22 00:05:00                                                1.0  
2019-11-22 00:10:00                                                2.0  
2019-11-22 00:15:00                                                3.0  
2019-11-22 00:20:00                                                4.0  
2019-11-22 00:25:00                                                5.0  
2019-11-22 00:30:00                                                6.0  
2019-11-22 00:35:00                                                7.0  
2019-11-22 00:40:00                                                8.0  
2019-11-22 00:45:00                                                0.0  
2019-11-22 00:50:00                                                0.0  
2019-11-22 00:55:00                                                0.0  
2019-11-22 01:00:00                                                0.0  
2019-11-22 01:05:00                                                0.0  
2019-11-22 01:10:00                                                0.0  
2019-11-22 01:15:00                                                0.0  

当我尝试的时候

    data[data<5]=0

它只返回大于5的值。任何帮助都会很好。你知道吗


Tags: 数据代码dataindexvalue赋值
2条回答

让我们试试这个:

df = pd.read_clipboard(index_col=0, sep='\s\s+')

df.index = pd.to_datetime(df.index)

grp = df['Value'].diff().lt(0).cumsum()

df_out = df.where(df.groupby(grp)['Value'].transform('max').gt(5), 0)
print(df_out)

输出:

                     Value
Index                     
2019-11-22 00:00:00    0.0
2019-11-22 00:05:00    1.0
2019-11-22 00:10:00    2.0
2019-11-22 00:15:00    3.0
2019-11-22 00:20:00    4.0
2019-11-22 00:25:00    5.0
2019-11-22 00:30:00    6.0
2019-11-22 00:35:00    7.0
2019-11-22 00:40:00    8.0
2019-11-22 00:45:00    0.0
2019-11-22 00:50:00    0.0
2019-11-22 00:55:00    0.0
2019-11-22 01:00:00    0.0
2019-11-22 01:05:00    0.0
2019-11-22 01:10:00    0.0
2019-11-22 01:15:00    0.0
​

试试这个:

filter = data["Value"].where(data["Value"] > 5, 0)
indices_with_6 = filter[filter == 6].index
for idx in indices_with_6:
    filter[idx - 5: idx] = [1., 2., 3., 4., 5.]
print(filter)
0     0
1     1
2     2
3     3
4     4
5     5
6     6
7     7
8     8
9     0
10    0
11    0
12    0
13    0
14    0
15    0
Name: Value, dtype: int64

相关问题 更多 >