Repeating values in a specific pattern in pandas dataframe column

user308827

I want to create a column in a pandas dataframe such that it contains values from 10 to 100 with an increment of 10. The value of 10 is assigned to the first 10 percent of the rows and the value of 20 is assigned to the next 10% of the rows and so on. Assuming that the dataframe has a length of 153, this is what I tried:

arr = range(10, 110, 10)
np.resize(arr, 153)

I get this: array([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30])

How can I fix this? I want the foll. result:

array([ 10, 10, 10, 10, 10, 10,... 100, 100, 100, 100])

mozway

Use math, generate a range of N numbers, compute the floor division by 10% of N and scale:

N = 153

a = (np.arange(N)//(N*0.1)+1)*10

# df = pd.DataFrame,({'col': a.astype(int)})

Or with. linspace:

N = 153
step = 10

a = (np.linspace(10, 100+step, N+1)//step*step)[:-1]

Output (as array):

Note that the two approaches don't round numbers the same way and can give a slightly different output if N is not a multiple of 10.

array([ 10,  10,  10,  10,  10,  10,  10,  10,  10,  10,  10,  10,  10,
        10,  10,  10,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,
        20,  20,  20,  20,  20,  30,  30,  30,  30,  30,  30,  30,  30,
        30,  30,  30,  30,  30,  30,  30,  40,  40,  40,  40,  40,  40,
        40,  40,  40,  40,  40,  40,  40,  40,  40,  40,  50,  50,  50,
        50,  50,  50,  50,  50,  50,  50,  50,  50,  50,  50,  50,  60,
        60,  60,  60,  60,  60,  60,  60,  60,  60,  60,  60,  60,  60,
        60,  70,  70,  70,  70,  70,  70,  70,  70,  70,  70,  70,  70,
        70,  70,  70,  70,  80,  80,  80,  80,  80,  80,  80,  80,  80,
        80,  80,  80,  80,  80,  80,  90,  90,  90,  90,  90,  90,  90,
        90,  90,  90,  90,  90,  90,  90,  90, 100, 100, 100, 100, 100,
       100, 100, 100, 100, 100, 100, 100, 100, 100, 100])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reshaping Pandas dataframe based on repeating values in a column

Group repeating pattern in pandas Dataframe

How to expand a list in a pandas dataframe without repeating other column values

Adding a column of repeating values to dataframe

Replace specific values in a dataframe column using Pandas

Replace specific column values in pandas dataframe

pandas dataframe filter by sequence of values in a specific column

Replace specific values in a dataframe by column mean in pandas

create pandas dataframe with repeating values

Python Pandas: Split by pattern (across rows) in a specific column of DataFrame

Set values of pandas dataframe that match another dataframe on a specific column

Extracting specific column values in pandas DataFrame comparing another DataFrame

Replacing specific values in a Pandas dataframe basing on the values of another column

How to fill in missing values in Pandas dataframe according to pattern in column?

Pandas: delete only specific duplicate column values in a pandas dataframe

Repeating columns based on corresponding column values and rows based on total values in pandas dataframe

Add Suffix to Dataframe with Specific Repeating Column Name

How to iterate over repeating values in DataFrame column?

Finding monthly repeating values in dataframe (pandas)

Create repeating values index in Pandas dataframe

How to remove repeating values in a pandas dataframe

Replacing column values in a pandas dataframe based if it contains a specific substring

Set specific rows values to column in pandas dataframe using list comprehension

How to sum certain values in a pandas column DataFrame in a specific date range

Delete rows if there are null values in a specific column in Pandas dataframe

Pandas - Create Separate Columns in DataFrame Based on a Specific Column's Values

Filter a pandas Dataframe based on specific month values and conditional on another column

Finding a timedelta in pandas dataframe based upon specific values in one column

Replacing specific column values after removing duplicates in a pandas dataframe

TOP Ranking

HotTag

Archive