Вот еще один случай, когда сырые типы вас укусят:
public class StrangeClass<T> {
@SuppressWarnings("unchecked")
public <X> X getSomethingElse() {
return (X)"Testing something else!";
}
public static void main(String[] args) {
final StrangeClass<Object> withGeneric = new StrangeClass<>();
final StrangeClass withoutGeneric = new StrangeClass();
final String value1,
value2;
// Works
value1 = withGeneric.getSomethingElse();
// Produces compile error:
// incompatible types: java.lang.Object cannot be converted to java.lang.String
value2 = withoutGeneric.getSomethingElse();
}
}
Как уже упоминалось в принятом ответе, вы теряете всю поддержку дженериков в коде необработанного типа. Каждый параметр типа преобразуется в его стирание (которое в приведенном выше примере просто Object
).
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
len(df.index)
, 381ns vs df.shape
1.17us. Ойр чего-то не хватает?
– jezrael
3 January 2018 в 06:15
inplace=True
. – alhuelamo 24 February 2016 в 14:03inplace=True
метод возвращает None – alyaxey 30 October 2017 в 11:41