Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想构建一个 Pandas 数据框,所有行都应该等于 df2 的列名:
df1 = pd.Dataframe( ???, index=df2.index, columns=df2.columns)
我已经尝试过,但它不起作用:
df1 = pd.Dataframe( np.repeat(df2.columns, df2.shape[0]) , index=df2.index, columns=df2.columns)
In [135]: df = pd.DataFrame([list('abc')], index=range(5), columns=list('abc')) In [136]: df Out[136]: 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]
因此,使用:
df1 = pd.Dataframe([df2.columns], index=df2.index, columns=df2.columns)