根据整数索引位置删除pandas DataFrame中的一行

3

我有时间序列数据,其中有重复的时间戳索引,但我只想基于整数位置删除单个行。例如,如果我有以下内容:

import numpy as np
import pandas as pd

dates       = pd.to_datetime(["2015-10-22 09:40:00","2015-10-22 09:40:00","2015-10-22 09:40:00","2015-10-22 09:50:00","2015-10-22 10:00:00"])
data_rand   = np.random.rand(len(dates),3)
col_head    = ['A','B','C']

df          = pd.DataFrame(data=data_rand, index=dates, columns=col_head)

print(df)
rowindex    = 1

df.drop(df.index[rowindex], inplace=True)
#df.drop(df.index.iloc[[rowindex]], inplace=True)

print(df)

数据输出的数据框如下所示:

                            A         B         C
2015-10-22 09:40:00  0.755642  0.797471  0.366410
2015-10-22 09:40:00  0.475411  0.629229  0.733368
2015-10-22 09:40:00  0.003278  0.461901  0.184833
2015-10-22 09:50:00  0.803465  0.218510  0.864337
2015-10-22 10:00:00  0.153356  0.950724  0.249950

现在,如果我想删除第二行,我会使用drop函数,但是因为还有两个具有完全相同索引的标签,所有三个都将被删除。有没有办法只删除三个重复时间戳中间的那个?我更喜欢在不重置索引的情况下完成这个操作。
我希望数据看起来像这样:
                            A         B         C
2015-10-22 09:40:00  0.755642  0.797471  0.366410
2015-10-22 09:40:00  0.003278  0.461901  0.184833
2015-10-22 09:50:00  0.803465  0.218510  0.864337
2015-10-22 10:00:00  0.153356  0.950724  0.249950
3个回答

1
您可以使用 ilocloc,例如:
In [5055]: idx = np.ones(len(df.index), dtype=bool)

In [5057]: idx[rowindex] = False

In [5058]: df.iloc[idx]     # or df.loc[idx]
Out[5058]:
                            A         B         C
2015-10-22 09:40:00  0.704959  0.995358  0.355915
2015-10-22 09:40:00  0.151127  0.398876  0.240856
2015-10-22 09:50:00  0.343456  0.513128  0.666625
2015-10-22 10:00:00  0.105908  0.130895  0.321981

细节
In [5059]: df
Out[5059]:
                            A         B         C
2015-10-22 09:40:00  0.704959  0.995358  0.355915
2015-10-22 09:40:00  0.762548  0.593177  0.691702
2015-10-22 09:40:00  0.151127  0.398876  0.240856
2015-10-22 09:50:00  0.343456  0.513128  0.666625
2015-10-22 10:00:00  0.105908  0.130895  0.321981

我喜欢这个解决方案的灵活性。有没有办法使用类似于drop中inplace选项的东西?否则,我将不得不重新保存数据框(即,您的最后一行将是df = df.iloc[idx])。 - Moose Drool
不,我不这么认为。 - Zero

1
你可以使用整数位置来查找行的名称(即索引):
df = df.drop(df.iloc[i].name)

0
使用np.arange和iloc选择除rowindex之外的行。非常类似于删除rowindex,即(如果考虑删除多个行索引,请参考@Zero的答案)
rowindex = 2
ndf = df.iloc[~(np.arange(df.shape[0]) == rowindex )]

输出:

                       A         B         C
2015-10-22 09:40:00  0.568431  0.302549  0.497309
2015-10-22 09:40:00  0.683263  0.916699  0.108929
2015-10-22 09:50:00  0.751543  0.480892  0.797728
2015-10-22 10:00:00  0.282703  0.433418  0.009757
df
                            A         B         C
2015-10-22 09:40:00  0.568431  0.302549  0.497309
2015-10-22 09:40:00  0.683263  0.916699  0.108929
2015-10-22 09:40:00  0.495492  0.232836  0.436861
2015-10-22 09:50:00  0.751543  0.480892  0.797728
2015-10-22 10:00:00  0.282703  0.433418  0.009757

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接