1

Имеется dataframe, подобный данному:

  col1    col2 
0 20      489
1 40      528
2 50      662

Нужно получить это:

 col1    col2
0 10     0
1 20     489
2 30     0
3 40     528
4 50     662

1 ответ 1

1

Воспользуйтесь DataFrame.reindex().

Исходный DataFrame:

In [27]: df
Out[27]:
   col1  col2
0    20   489
1    40   528
2    50   662

Решение:

import numpy as np

res = df.set_index('col1').reindex(np.arange(10, 60, 10), fill_value=0).reset_index()

Результат:

In [29]: res
Out[29]:
   col1  col2
0    10     0
1    20   489
2    30     0
3    40   528
4    50   662

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.