0

DataFrame D1 like:

enter image description here

DataFrame D2 like:

enter image description here

I want to merge them like:

enter image description here

I used pd.concat, get this(This is not what I want):

enter image description here

How can I append C,D..... to every row in a Pandas DataFrame D1.

user96564
  • 1,578
  • 5
  • 24
  • 42
Anna
  • 261
  • 1
  • 2
  • 12
  • With the merge function build into pandas ([docs](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html)) – MikeMajara Nov 21 '19 at 09:57

1 Answers1

1

If you always have only one row in the second dataframe, you can do:

for col in df2:
   df1[col] = np.tile(df2[col].values, len(df1))

Note that tile just repeats the array elements up to a certain length (here len(df1)). This is equivalent to

df2[col].values.tolist() * len(df1)

If you need something more performant and clean, have a look at pandas' merge function.

Derlin
  • 9,572
  • 2
  • 32
  • 53