pandas - How to reset an unordered index to an ordered one in python? -


i have dataframe below

textdata

   id   user_category   operator   circle   0  23       1           vodafone   mumbai   1  45       2           airtel     andhra   2  65       3           airtel     chennai   3  23       6           vodafone   mumbai   4  45       1           airtel     gurgaon   5  65       3           airtel     ongole   6  23       4           vodafone   mumbai   7  45       1           airtel     telangana   8  65       3           airtel     chennai   

in data 1,2,4,6 in user category transactional , 3 in user_category promotional data.so have divided using following commands

transactional = textdata[textdata['user_category'].isin([1,2,4,6])] promotional = textdata[textdata['user_category'].isin([1])] 

so got output transactional , promotional below

transactional

   id   user_category   operator   circle   0  23       1           vodafone   mumbai   1  45       2           airtel     andhra       3  23       6           vodafone   mumbai   4  45       1           airtel     gurgaon   6  23       4           vodafone   mumbai   7  45       1           airtel     telangana   

promotional

    id   user_category   operator   circle   2  65       3           airtel     chennai    8  65       3           airtel     chennai   5  65       3           airtel     ongole   

but expecting order index expected output:

transactional

   id   user_category   operator   circle   0  23       1           vodafone   mumbai   1  45       2           airtel     andhra       2  23       6           vodafone   mumbai   3  45       1           airtel     gurgaon   4  23       4           vodafone   mumbai   5  45       1           airtel     telangana   

promotional

    id   user_category   operator   circle   1  65       3           airtel     chennai    2  65       3           airtel     chennai   3  65       3           airtel     ongole   

this how tried

transactional.reset_index(inplace = true)

and how got

transactional

    index  id   user_category   operator   circle    0    0    23       1           vodafone   mumbai    1    1    45       2           airtel     andhra        2    3    23       6           vodafone   mumbai    3    4    45       1           airtel     gurgaon    4    6    23       4           vodafone   mumbai    5    7    45       1           airtel     telangana   

but expecting in following way

transactional

   id   user_category   operator   circle   0  23       1           vodafone   mumbai   1  45       2           airtel     andhra       2  23       6           vodafone   mumbai   3  45       1           airtel     gurgaon   4  23       4           vodafone   mumbai   5  45       1           airtel     telangana   

please me how can this.

but don't suggest me way

del transactional['index'] 

thanks in advance

use drop=true option of reset_index.

drop : boolean, default false. not try insert index dataframe columns. resets index default integer index

so, instead of calling:

transactional.reset_index(inplace = true) 

do:

transactional.reset_index(inplace = true, drop=true) 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -