일부 행을 제거 할 데이터 프레임이 있습니다. 결과적으로 색인이 다음과 같은 데이터 프레임을 얻게됩니다 : [1,5,6,10,11]
그리고 [0,1,2,3,4]
으로 재설정하고 싶습니다. 내가 어떻게 해?팬더 데이터 프레임에서 인덱스를 재설정하는 방법은 무엇입니까?
df = df.reset_index()
del df['index']
다음은 작동하지 않습니다 :
df = df.reindex()
일부 행을 제거 할 데이터 프레임이 있습니다. 결과적으로 색인이 다음과 같은 데이터 프레임을 얻게됩니다 : [1,5,6,10,11]
그리고 [0,1,2,3,4]
으로 재설정하고 싶습니다. 내가 어떻게 해?팬더 데이터 프레임에서 인덱스를 재설정하는 방법은 무엇입니까?
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 Source - 가장 빠른 것은'len (df.index)', 381ns 대'df.shape' 1.17us입니다. Oyr 뭔가 빠졌어요? – jezrael
1 'drop = True' – Rhubarb
데이터 프레임을 같은 변수에 재 할당하는 대신에'inplace = True' 인수를 설정할 수 있습니다. – ahuelamo
'inplace = True'의 경우, 메소드는 None을 반환합니다. – alyaxey