简体   繁体   English

在熊猫中使用df.drop时,使行的索引移动

[英]When using df.drop in pandas make the index of rows shift

I am working on a program where I take a random value find coordinates that are similar in distance to them add them to a group and remove them from the Dataframe. 我正在开发一个程序,在该程序中我采用随机值查找与它们距离相似的坐标,将它们添加到组中并将其从数据框中删除。

So assuming I have the list of these coordinates 所以假设我有这些坐标的列表

在此处输入图片说明

They should be grouped so that index 他们应该分组以便索引

  • 0 and 1 are together 0和1在一起
  • 2 and 4 are together 2和4在一起
  • 3 is alone 3一个人

The issue im running into is that the initial index is chosen at random for the count of rows using 我遇到的问题是使用以下方法随机选择初始索引作为行数

randInt = random.randint(0, count_row - 1)

and when I remove a coordinate point from the Dataframes it doesn't shift the others up like the following picture. 当我从数据框中删除一个坐标点时,其他坐标不会像下面的图片那样向上移动。

在此处输入图片说明

I am currently deleting the points using the following commands. 我目前正在使用以下命令删除这些点。

The random index chosen is added to a list then removed from Dataframe using 所选的随机索引将添加到列表中,然后使用

df.drop(current, inplace=True)

Then if a point is within a certain distance it is added to the list and removed using (where index is the index of the point in the Dataframes when looping through) 然后,如果某个点在一定距离内,则将其添加到列表中并使用删除(其中index是遍历时数据帧中该点的索引)

df.drop(index, inplace=True)

So my question is, how can I make it so that when a point its removed it shifts the Ids up with it? 所以我的问题是,我如何才能做到这一点,以便在删除点时将其ID向上移动?

尝试重置索引

df = df.reset_index(drop=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM