How do I stack the following 2 dataframes:
df1 hzdept_r hzdepb_r sandtotal_r 0 0 114 0 1 114 152 92.1 df2 hzdept_r hzdepb_r sandtotal_r 0 0 23 83.5 1 23 152 45
to give the following result:
hzdept_r hzdepb_r sandtotal_r 0 0 114 0 1 114 152 92.1 2 0 23 83.5 3 23 152 45
Using the pandas merge operations does not work since it just lines the dataframes horizontally (and not vertically, which is what I want)
DataFrame - stack() function The stack() function is used to stack the prescribed level(s) from columns to index. Return a reshaped DataFrame or Series having a multi-level index with one or more new inner-most levels compared to the current DataFrame.
To append the rows of one dataframe with the rows of another, we can use the Pandas append() function. With the help of append(), we can append columns too. Let's take an example and see how to use this method.
Merge. Another widely used function to combine DataFrames is merge(). Concat() function simply adds DataFrames on top of each other or adds them side-by-side.
pandas. DataFrame. append() method is used to append one DataFrame row(s) and column(s) with another, it can also be used to append multiple (three or more) DataFrames.
In [5]: a = pd.DataFrame(data=np.random.randint(0,100,(2,5)),columns=list('ABCDE')) In [6]: b = pd.DataFrame(data=np.random.randint(0,100,(2,5)),columns=list('ABCDE')) In [7]: c = pd.concat([a,b],ignore_index=True) In [8]: c Out[8]: A B C D E 0 12 56 62 35 20 1 10 71 63 0 70 2 61 72 29 10 71 3 88 82 39 73 94
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With