2013-12-10 8 views

답변

318

reset_index() 당신을 위해 무엇을 찾고있는 사람입니다

는 작동하는 것 같다 다음

을 추가했습니다. 당신이 열로 저장하지 않으려면, 다음을 수행하십시오

df = df.reset_index(drop=True) 
+47

1 'drop = True' – Rhubarb

+53

데이터 프레임을 같은 변수에 재 할당하는 대신에'inplace = True' 인수를 설정할 수 있습니다. – ahuelamo

+1

'inplace = True'의 경우, 메소드는 None을 반환합니다. – alyaxey

8

또 다른 솔루션 할당 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 
+0

@Outcast Source - 가장 빠른 것은'len (df.index)', 381ns 대'df.shape' 1.17us입니다. Oyr 뭔가 빠졌어요? – jezrael

관련 문제