Quantcast
Viewing all articles
Browse latest Browse all 14069

how to create rows to fill in to complete sequence of numbers in pandas dataFrames or Series ?? or with Numpy?

My problem is simple to visualize:i have a dataframe with numbers(Not the index), and one other column:

 1    'chicken' 2    'pig' 4    'cow'

Now you see that from 2 jumps to four, i wanted to add a number 3, even with the str column empty, i just need to complete the sequence of numbers for other projects, so as to look like this:

 1    'chicken' 2    'pig' 3    '' 4    'cow'

the other project takes the len() of the frame to pick specific numbers, but assumes the length includes the numbers that make for that length, and if there are missing numbers on the sequence, it's gonna choose the wrong items. for example it will think a len(300) has only from 1 to 300, but the frame includes number 322.

Do you know of any method in Pandas, or Numpy that can deal with missing natural sequence of integers ?

i think: first Sort by the numbers, then run a for loop and check if the current number equals the previous plus one plus missed_counter (which would be 0 initially), if not, then append to the bottom, do += 1 to missed_counter....after the for loop is done, sort by numbers again. I just thought of this so idk if it would work like that, and if this is super slow for big data.


Viewing all articles
Browse latest Browse all 14069

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>