pythonrepeatrep

How to repeat different numeric values into a dataframe in python (similar to rep() in R)


I would like to make a dataframe in python with two values (red and yellow) that repeat different quantities (1599 and 4898). The column should first have red 1599 times following yellow 4898 times.

In R it would look like:

colors <- c(rep("Red",1599),rep("Yellow",4898))

How do I make the same exact column in python?


Solution

  • From your R code, you did not use numerical values to encode textual values of red and yellow, so I still use string here.

    This is what comes to my mind:

    import pandas as pd
    import numpy as np
    
    red_values = np.array(['red'] * 1599)
    red_part = pd.DataFrame(red_values)
    yellow_values = np.array(['yelllow'] * 4898)
    yellow_part = pd.DataFrame(yellow_values)
    my_dataframe = pd.concat([red_part, yellow_part], axis=0)
    

    The result will be like: