使用python在dataframe中标记特定的连续值

2024-03-28 21:02:27 发布

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

我有一个多列的数据帧。我需要在第2列中将第4个值标记为第8个值,表示这些连续行的值等于0.1

ID           Column2      Column3
1              2.6            9
2              1.9            7
3              4.7            5
4              0.1            8     
5              0.1            6
6              0.1            2
7              0.1            8
8              0.1            7
9              3.0            4
10             1.2            9
11             2.8            7
12             3.1            9
13             1.0            3
14             2.4            4
15             3.2            5
n             nth value      nth value

我使用了下面的代码,它给了我TypeError。有人能帮忙吗?你知道吗

代码:

import pandas as pd
df = pd.read_csv('file.csv')

for row, index in df_May.iterrows():
    if row['Column2'] == row['Column2'].shift(1) | row['Column2'] == 0.1:
        print index,row

TypeError: 'int' object has no attribute '__getitem__'

Tags: csv数据代码标记iddfindexvalue
1条回答
网友
1楼 · 发布于 2024-03-28 21:02:27

您需要:

df.loc[(df['Column2'].diff()==0) | (df['Column2'].diff(-1)==0)]

输出:

    ID  Column2 Column3
3   4   0.1       8
4   5   0.1       6
5   6   0.1       2
6   7   0.1       8
7   8   0.1       7

相关问题 更多 >