【发布时间】:2019-01-08 08:14:16
【问题描述】:
我的大脑被锁定了。我收到下面的错误,不明白为什么,索引是 int64 所以??? .我想浏览数据集并找到所有更改的行并将它们的索引位置记录到一个数组中。然后用数组依次去每个位置,打印出接下来的两行就得到下一个位置。
TypeError: slice indices must be integers or None or have an __index__ method
这里是代码段和数据。
import pandas as pd
data = [['edward',1],['edward',0],['edward',0],['norm',1],['norm',0],['norm',0] ]
df_names = pd.DataFrame( data )
criteria = df_names[1] == 1
df_just_changes = df_names[ criteria ]
print(df_names)
print(df_just_changes .index)
print("type of df_just_changes.index {} ".format(type( df_just_changes.index ) ) )
print("type of df_just_changes.index[0] {} ".format(type( df_just_changes.index[0] ) ) )
print( df_names.values[df_just_changes.index : 2 ])
【问题讨论】:
-
你到底想做什么?您是否试图掩盖“已更改”的唯一行?
-
df_just_changes.index返回一个数组,而不是int。你想达到什么目的? -
对不起,我更改了问题以显示我正在尝试做的事情,即通过更大的数据找到列为 1 的位置,然后打印出接下来的两行,然后返回下一个值在数组中并做同样的事情
标签: python-3.x pandas slice