Home > database >  How can I duplicate the first row of a dataframe to the length of the dataframe using Pandas
How can I duplicate the first row of a dataframe to the length of the dataframe using Pandas

Time:03-16

I would like duplicate the first row of this dataframe but keep the index and the first column the same. I cant find a way of leaving the first column the same.

This is just a minimized example I have 1000 rows that I am working with.

This is what is tried:

import numpy as np
import pandas as pd
df = pd.DataFrame({'X':[1,2,3,4,5], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]});

df.iloc[np.arange(1).repeat(len(df))].reset_index(drop=True)

This is what I am looking for

({'X':[1,2,3,4,5], 'Y':[84,84,84,84,84],'Z':[86,86,86,86,86]});

CodePudding user response:

This just works fine

df.iloc[1:, 1:] = df.iloc[0, 1:].values

CodePudding user response:

Just specify the columns you want, and use .loc:

cols = ['Y', 'Z']
df.loc[:, cols] = df.loc[0, cols].to_numpy()

Output:

>>> df
   X   Y   Z
0  1  84  86
1  2  84  86
2  3  84  86
3  4  84  86
4  5  84  86

Or, if you would rather specify the columns you don't want, use df.columns.difference:

cols = df.columns.difference(['X'])
df.loc[:, cols] = df.loc[0, cols].to_numpy()

CodePudding user response:

Why not

df['Y'] = [df['Y'].iloc[0]]*len(df)
df['Z'] = [df['Z'].iloc[0]]*len(df)
  • Related