在熊猫中连接数据框

时间:2021-04-17 14:57:39

标签: python pandas dataframe merge concatenation

有没有办法连接下面的 2 个数据帧:

我将有一个带有标题的新数据框:

时间戳调整后报告EPS估计EPS

并且报告的EPS和估计的EPS将根据以下各自的值保持不变:

1 月 1 日 - 3 月 31 日、4 月 1 日 - 6 月 30 日、7 月 1 日 - 9 月 30 日、1 月 1 日 - 12 月 31 日的时间戳?

2 个数据框:

https://gyazo.com/38b50a3d7eb138521c89ac93e31b948d

https://gyazo.com/d0ad4c884bf818d0e32d50134de08b39

1 个答案:

答案 0 :(得分:0)

考虑以下来自 this link 的解释和示例代码。

<块引用>

当我们连接DataFrames时,我们需要指定轴。 axis=0 告诉 pandas 在第一个 DataFrame 下堆叠第二个 DataFrame。它 会自动检测列名是否相同 将相应地堆叠。 axis=1 将在第二个中堆叠列 DataFrame 到第一个 DataFrame 的右侧。堆叠数据 垂直,我们需要确保我们有相同的列和 两个数据集中的关联列格式。当我们水平堆叠时, 我们想确保我们所做的事情有意义(即数据是 以某种方式相关)。

# Stack the DataFrames on top of each other
vertical_stack = pd.concat([survey_sub, survey_sub_last10], axis=0)

# Place the DataFrames side by side
horizontal_stack = pd.concat([survey_sub, survey_sub_last10], axis=1)