[Python] Need to keep data in two columns connected, while repeating across 100 rows

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a data set to prepare it for modeling in python. Here is my issue: I have a group of two columns that I need to keep connected (ex. a = ...) but repeat it throughout the data set. See below:

    col 1 col 2 col 3
    -0.363965 a 1.04344
    0.566 b -1.042184
    -1.734601 c -1.072206
    -0.944462 d -0.981626
    -1.15268 e -1.060917
    -1.734601
    -0.944462
    -1.15268
    -0.489548
    0.682
    0.698
    0.404
    0.852
    0.450439
    0.630066
    0.564755
    0.534752

    I've tried pd.merge, np.repeat, etc. All I can get is either repetition of the first value in column 3 or NaN or errors. Is this even possible in python?

    Ideally, it would look like this:

    col 1 col 2 col 3
    -0.363965 a 1.04344
    0.566 b -1.042184
    -1.734601 c -1.072206
    -0.944462 d -0.981626
    -1.15268 e -1.060917
    -1.734601 b -1.042184
    -0.944462 b -1.042184
    -1.15268 c -1.072206
    -0.489548 a 1.04344
    0.682 c -1.072206
    0.698 e -1.060917
    0.404 a 1.04344
    0.852 b -1.042184
    0.450439 c -1.072206
    0.630066 a 1.04344
    0.564755 e -1.060917
    0.534752 a 1.04344

    I've tried pd.merge, np.repeat, etc. All I can get is either repetition of the first value in column 3 or NaN or errors. Is this even possible in python?

    For additional context - column 3 are the dummy variables created for the category of column 2. I want to be be able to model column 1 values (that are associated with column 2 categories) in a linear regression model with other interactions and additives (all continuous) and show the predicted performance with confidence interval and prediction intervals in a graph.

    Continue reading...

Compartilhe esta Página