我有一个包含多列的数据框。我需要在Column2中将第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__'
答案 0 :(得分:4)
你需要:
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