Updated 2022-05-22 1
Viewed 5 times
0

I have a empty dataframe with a datetime index like this:

>>> idx_df
Empty DataFrame
Columns: []
Index: [2020/1/1, 2020/1/1, 2020/1/2, 2020/1/2, 2020/1/3, 2020/1/3, 2020/1/3, 2020/1/3, 2020/1/3, 2020/1/4, 2020/1/4, 2020/1/4, 2020/1/5, 2020/1/5, 2020/1/5, 2020/1/5]

I want merge another dataframe(df2) with unequal index length and with some duplicate index value to this empty dataframe.

df2                        desire result    
             transin        date        transin
date                        2020/1/1    1.0 
2020/1/1      1.0           2020/1/1    
2020/1/2      1.0           2020/1/2    1.0 
2020/1/2      1.0           2020/1/2    1.0
2020/1/3      1.0           2020/1/3    1.0 
2020/1/3      1.0           2020/1/3    1.0 
2020/1/3      1.0           2020/1/3    1.0 
2020/1/3      1.0           2020/1/3    1.0 
2020/1/3      1.0           2020/1/3    1.0 
2020/1/4      5.0           2020/1/4    5.0 
2020/1/4      6.0           2020/1/4    6.0 
2020/1/5     10.0           2020/1/4    
2020/1/5     11.0           2020/1/5    10.0 
2020/1/5     13.0           2020/1/5    11.0 
2020/1/5     16.0           2020/1/5    13.0 

but I have tried:

pd.merge(idx_df, df2,right_index=True,left_index=True)
idx_df.join(df2)
pd.concat([idx_df, df2],axis=1]

all of these method can not give me desire result, so what should I do?

🔴 No definitive solution yet
📌 Solution 1
0

You can just do reindex

out = df2.reindex(idx_df.index)