Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas equivalent of R's cbind (concatenate/stack vectors vertically)

suppose I have two dataframes:

import pandas .... .... test1 = pandas.DataFrame([1,2,3,4,5]) .... .... test2 = pandas.DataFrame([4,2,1,3,7]) .... 

I tried test1.append(test2) but it is the equivalent of R's rbind.

How can I combine the two as two columns of a dataframe similar to the cbind function in R?

like image 875
uday Avatar asked Feb 18 '15 23:02

uday


People also ask

How do I concatenate horizontally in pandas?

To concatenate DataFrames horizontally in Pandas, use the concat(~) method with axis=1 .

How do I concatenate values in pandas?

By use + operator simply you can concatenate two or multiple text/string columns in pandas DataFrame. Note that when you apply + operator on numeric columns it actually does addition instead of concatenation.

What is the difference between merging and concatenation in pandas?

Concat function concatenates dataframes along rows or columns. We can think of it as stacking up multiple dataframes. Merge combines dataframes based on values in shared columns. Merge function offers more flexibility compared to concat function because it allows combinations based on a condition.

Does PD concat match columns?

Pandas can concat dataframe while keeping common columns only, if you provide join='inner' argument in pd.


1 Answers

test3 = pd.concat([test1, test2], axis=1) test3.columns = ['a','b'] 
like image 97
cphlewis Avatar answered Sep 19 '22 23:09

cphlewis