I have values in a column that are 1 to 50. What is the easiest way to convert every 5 to scale back to 1-10? (e.g. [1,2,3,4,5] = 1, [6,7,8,9,10] = 2 )
The input object is unclear, but you need to use a floor division.
Since python counts from 0 but you from 1, first subtract 1, then divide, finally add 1 again:
Example with numpy:
import numpy as np
inpt = np.arange(1, 51)
out = (inpt-1)//5+1
Output:
array([ 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4,
4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7,
7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10])
In pure python:
inpt = list(range(1, 51))
out = [(x-1)//5+1 for x in inpt]
In pandas:
import pandas as pd
df = pd.DataFrame({'col': range(1, 51)})
df['out'] = df['col'].sub(1).floordiv(5).add(1)