0

If I have a pandas data frame like this:

     0   1   2 
 0   0   0   0
 1   1   0   1
 2   0   0   1
 3   1   1   0

and an pandas data frame like this:

     0   1   2
 0   0   2   3 

How do I concatenate this array to each row such that I get a new pandas data frame like this:

     0   1   2   3   4   5
 0   0   0   0   0   2   3
 1   1   0   1   0   2   3
 2   0   0   1   0   2   3
 3   1   1   0   0   2   3
Zmann3000
  • 806
  • 5
  • 13
  • Possible duplicate of [how to combine two data frames in python pandas](https://stackoverflow.com/questions/12850345/how-to-combine-two-data-frames-in-python-pandas) – emilaz Apr 15 '19 at 16:32

2 Answers2

2

There was a deleted answer which was very closed.

# merge the data frame
df = pd.concat([df1, df2], axis=1, sort=False)

# rename dataframe to advoid duplications
df.columns = range(len(df.columns))

# fill na's in the columns of df2
df[-len(df2.columns):].ffill(inplace=True)
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

Can assign multiple static values with a dict:

df1.columns = ['3', '4', '5']
df = df.assign(**df1.to_dict('index')[0])
# If need to be int names: df.columns = df.columns.astype(int)

   0  1  2  3  4  5
0  0  0  0  0  2  3
1  1  0  1  0  2  3
2  0  0  1  0  2  3
3  1  1  0  0  2  3
ALollz
  • 57,915
  • 7
  • 66
  • 89