New Dataframe from two columns in different data frames

43 Views Asked by At

I need to create a new data frame from columns of two other data frames

df1 = A,
      B,
      C

df2 = X, 
      Y, 
      Z 

the indexes are same, when i use the code what happens is this

#df3 = pd.concat(df['A'], df['B'], axis=1, keys=['A', 'B'])

df3 = A, 
      B, 
      C, 
      Nan, X 
      Nan, Y
      Nan, Z

I need the X,Y,Z next to A,B,C

Concat and DataFrame

Edit 1: 2nd DF is a series (i was able to convert to series from a list), essentially struggling to add a series and DF

1

There are 1 best solutions below

1
e-motta On

You need to concatenate on axis=1.

Note that concat takes a list of pandas.DataFrame or pandas.Series as the first argument.

They keys argument isn't necessary, unless you need hierarchical columns.

df = pd.concat([df1, df2], axis=1)
  col1 col2
0    A    X
1    B    Y
2    C    Z