Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Repeat same row Pandas Dataframe Construction

I want to build a Pandas Dataframe and all rows should be equal to the column names of df2:

df1 = pd.Dataframe( ???, index=df2.index, columns=df2.columns)

I have tried with this but it doesn't work:

df1 = pd.Dataframe(  np.repeat(df2.columns, df2.shape[0])  , index=df2.index, columns=df2.columns)
like image 457
prre72 Avatar asked Mar 20 '23 04:03


1 Answers

In [135]: df = pd.DataFrame([list('abc')], index=range(5), columns=list('abc'))

In [136]: df
   a  b  c
0  a  b  c
1  a  b  c
2  a  b  c
3  a  b  c
4  a  b  c

[5 rows x 3 columns]

Therefore, use:

df1 = pd.Dataframe([df2.columns], index=df2.index, columns=df2.columns)
like image 73
unutbu Avatar answered Mar 31 '23 23:03