Add 1 - 5 to rows while taking into account the number of rows (may also be called tiling/binning) in Pandas, Python

2835 views python
2

I have a dataframe that looks like this which is in order:

enter image description here

I'm not sure what the technical term is (tiling I think) but I want to add a new column that has a string of numbers ranging from 1 to 5 to it that evenly distribute as best as possible, so my dataframe looks something like this:

enter image description here

How do I go about doing this?

Thanks in advance.

EDIT 2018-11-08: Was asked to paste the actual data into the question. How my data looks currently:

Name Score Harry 43 Sally 234 Mary 54 John 34 Francis 12 Devon 43 James 54 Holly 65 Molly 23 Nancy 12 Ben 32

Here is how I want it to look:

Name Score Tile Harry 43 1 Sally 234 1 Mary 54 1 John 34 2 Francis 12 2 Devon 43 3 James 54 3 Holly 65 4 Molly 23 4 Nancy 12 5 Ben 32 5

answered question

Are they in order, or do you want them to be randomly sampled?

Ummmm why 1 repeat 3 times ? Also , can you post your sample data here (Not picture )

@ALollz they are in order

@W-B I'm not sure how else it would distribute if the amount of rows you have doesn't match your range. I shall paste the data in now...

It's just a matter of whether 1 should be repeated 3 times, or 5 (or any other number)

1 Answer

8

Seems like you need qcut

pd.qcut(np.arange(len(df)),5,labels=[1,2,3,4,5])
Out[175]: 
[1, 1, 1, 2, 2, ..., 3, 4, 4, 5, 5]

posted this

Have an answer?

JD

Please login first before posting an answer.