将行数据框附加并填充到更大的数据框以创建新列

Append and fill row dataframe to a larger dataframe to create new columns

我有一个更大的数据框如下:

df1

Month      Bkt    Cion
2021-06-01 Ofk    0.07
2021-06-01 Ofk    0.23
2021-06-01 Onk    0.10
2021-06-01 Onk    0.00

我有另一个非常大的单行数据框:

row.to_frame().T
        S1   S2
1       13   65

我想将 row 附加到 df1 以获得以下内容:

Month      Bkt    Cion   S1   S2
2021-06-01 Ofk    0.07   13   65
2021-06-01 Ofk    0.23   13   65
2021-06-01 Onk    0.10   13   65
2021-06-01 Onk    0.00   13   65

我不确定如何处理上述内容?

一个选项是将 df2Index.repeat + loc 缩放到与 df1 相同的长度:

df1[df2.columns] = df2.loc[df2.index.repeat(len(df1))].values

df1:

        Month  Bkt  Cion  S1  S2
0  2021-06-01  Ofk  0.07  13  65
1  2021-06-01  Ofk  0.23  13  65
2  2021-06-01  Onk  0.10  13  65
3  2021-06-01  Onk  0.00  13  65

df2 列上 concat + ffill 的另一个选项:

new_df = pd.concat((df1, df2), axis=1)
new_df[df2.columns] = new_df[df2.columns].ffill()

new_df:

        Month  Bkt  Cion    S1    S2
0  2021-06-01  Ofk  0.07  13.0  65.0
1  2021-06-01  Ofk  0.23  13.0  65.0
2  2021-06-01  Onk  0.10  13.0  65.0
3  2021-06-01  Onk  0.00  13.0  65.0

使用的帧数:

df1 = pd.DataFrame({
    'Month': ['2021-06-01', '2021-06-01', '2021-06-01', '2021-06-01'],
    'Bkt': ['Ofk', 'Ofk', 'Onk', 'Onk'],
    'Cion': [0.07, 0.23, 0.1, 0.0]
})

df2 = pd.DataFrame({'S1': [13], 'S2': [65]})