Search code examples
pythonpandasdataframerow

Pandas create dataframe from one row


Let us say i have some dataframe df and I want to create a new dataframe new_df with n rows each being the same as row idx from df. Is there way to do this in fewer lines compared to:

import pandas as pd

df = pd.DataFrame()
new_df = pd.DataFrame()


for i in range(n):
    new_df.loc[i] = df.iloc[idx]

thanks


Solution

  • You can use repeat:

    N = 5
    new_df = df.loc[df.index.repeat(N)]
    # or for a particular row idx
    new_df = df.loc[df.loc[idx].index.repeat(N)]
    

    Or, for a new index reset_index with drop=True:

    new_df = df.loc[df.index.repeat(N)].reset_index(drop=True)
    # or for a particular row idx
    new_df = df.loc[df.loc[idx].index.repeat(N)].reset_index(drop=True)
    

    NB. if you have many rows in the input and only want to repeat one or some. replace df.index.repeat(N) with df.loc[idx].index.repeat(N) of df.loc[['idx1', 'idx2', 'idx3']].index.repeat(N)

    Example input:

    df = pd.DataFrame([['A', 'B', 'C']])
    

    Output:

       0  1  2
    0  A  B  C
    1  A  B  C
    2  A  B  C
    3  A  B  C
    4  A  B  C