Olivia
Olivia

Reputation: 155

Concatenate dataframe in pandas

Is there a way to concatenate the 2 dataframes below such that:

I will have a new dataframe with headers:

Timestamp adjusted close reportedEPS estimatedEPS

and the reportedEPS and estimatedEPS will be constant according to the respective values from:

1 jan - 31 mar, 1 apr - 30 june, 1 july - 30 sep, 1 oct - 31 dec of the timestamps?

The 2 dataframes:

https://gyazo.com/38b50a3d7eb138521c89ac93e31b948d

https://gyazo.com/d0ad4c884bf818d0e32d50134de08b39

Upvotes: -1

Views: 1714

Answers (1)

Mohammad Moridi
Mohammad Moridi

Reputation: 367

Consider the following explanation and sample code from this link.

When we concatenate DataFrames, we need to specify the axis. axis=0 tells pandas to stack the second DataFrame UNDER the first one. It will automatically detect whether the column names are the same and will stack accordingly. axis=1 will stack the columns in the second DataFrame to the RIGHT of the first DataFrame. To stack the data vertically, we need to make sure we have the same columns and associated column format in both datasets. When we stack horizontally, we want to make sure what we are doing makes sense (i.e. the data are related in some way).

# 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)

Upvotes: 3

Related Questions