Reputation: 9919
Assume the following dataframe df1
:
A
1 123
2 456
3 765
4 987
5 456
6 234
7 111
8 222
9 677
10 55
and dataframe df2
:
B
1 333344
2 665577
3 889900
4 111111
I've tried doing this with pd.append(ignore_index=True)
and pd.concat(axis=1, ignore_index=True)
however the result is:
A B
1 123 333344
2 456 665577
3 765 889900
4 987 111111
5 456 NaN
6 234 NaN
7 111 NaN
8 222 NaN
9 677 NaN
10 55 NaN
However, my desired result would be:
A B
1 123 NaN
2 456 NaN
3 765 NaN
4 987 NaN
5 456 NaN
6 234 NaN
7 111 333344
8 222 665577
9 677 889900
10 55 111111
Do you have any suggestions on how to accomplish this?
Upvotes: 1
Views: 53
Reputation: 25259
You may use reindex
and shift
n = len(df1) - len(df2)
df_final = pd.concat([df1, df2.reindex(df1.index).shift(n)], axis=1)
Out[135]:
A B
1 123 NaN
2 456 NaN
3 765 NaN
4 987 NaN
5 456 NaN
6 234 NaN
7 111 333344.0
8 222 665577.0
9 677 889900.0
10 55 111111.0
Upvotes: 1
Reputation: 323326
Let us do
df2.index = df1.index[-len(df2):]
df = pd.concat([df1,df2],axis=1)
df
A B
1 123 NaN
2 456 NaN
3 765 NaN
4 987 NaN
5 456 NaN
6 234 NaN
7 111 333344.0
8 222 665577.0
9 677 889900.0
10 55 111111.0
Upvotes: 3