S Suen
S Suen

Reputation: 99

Concat two Pandas DataFrame column with different length of index

How do I add a merge columns of Pandas dataframe to another dataframe while the new columns of data has less rows? Specifically I need to new column of data to be filled with NaN at the first few rows in the merged DataFrame instead of the last few rows. Please refer to the picture. Thanks.

enter image description here

Upvotes: 1

Views: 2140

Answers (1)

jezrael
jezrael

Reputation: 862511

Use:

df1 = pd.DataFrame({
        'A':list('abcdef'),
        'B':[4,5,4,5,5,4],
})

df2 = pd.DataFrame({
        'SMA':list('rty')
})

df3 = df1.join(df2.set_index(df1.index[-len(df2):]))

Or:

df3 = pd.concat([df1, df2.set_index(df1.index[-len(df2):])], axis=1)
print (df3)
   A  B  SMA
0  a  4  NaN
1  b  5  NaN
2  c  4  NaN
3  d  5    r
4  e  5    t
5  f  4    y

How it working:

First is selected index in df1 by length of df2 from back:

print (df1.index[-len(df2):])
RangeIndex(start=3, stop=6, step=1)

And then is overwrite existing values by DataFrame.set_index:

print (df2.set_index(df1.index[-len(df2):]))
  SMA
3   r
4   t
5   y

Upvotes: 1

Related Questions