我有一個數據框從中刪除一些行。因此,我得到一個數據框,其索引是這樣的:[1,5,6,10,11]
,我想將它重置爲[0,1,2,3,4]
。我該怎麼做?如何重置熊貓數據框中的索引?
ADDED
以下似乎工作:
df = df.reset_index()
del df['index']
下不起作用:
df = df.reindex()
我有一個數據框從中刪除一些行。因此,我得到一個數據框,其索引是這樣的:[1,5,6,10,11]
,我想將它重置爲[0,1,2,3,4]
。我該怎麼做?如何重置熊貓數據框中的索引?
ADDED
以下似乎工作:
df = df.reset_index()
del df['index']
下不起作用:
df = df.reindex()
reset_index()
是你在找什麼。如果你不希望它保存爲一列這樣做,那麼做:
df = df.reset_index(drop=True)
另一種解決方案是分配RangeIndex
或range
:
df.index = pd.RangeIndex(len(df.index))
df.index = range(len(df.index))
這是更快:
df = pd.DataFrame({'a':[8,7], 'c':[2,4]}, index=[7,8])
df = pd.concat([df]*10000)
print (df.head())
In [298]: %timeit df1 = df.reset_index(drop=True)
The slowest run took 7.26 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 105 µs per loop
In [299]: %timeit df.index = pd.RangeIndex(len(df.index))
The slowest run took 15.05 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.84 µs per loop
In [300]: %timeit df.index = range(len(df.index))
The slowest run took 7.10 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 14.2 µs per loop
@Outcast來源 - 最快的是'len(df.index)',381ns vs'df.shape' 1.17us。 Oyr有什麼缺失? – jezrael
+1 'drop = True' – Rhubarb
而不是重新分配數據幀到同一個變量,你可以設置'inplace = True'參數。 – ahuelamo
請注意,如果'inplace = True',則方法返回無 – alyaxey