chen xin
chen xin

Reputation: 11

I want to combine two different size pandas.DataFrame

Based on the data below I would need to achieve the following:

DataFrame1

index1 value1
1      one
2      two
3      three
4      four
5      five
6      six
7      seven

DataFrame2

index2 value2
3      just
6      done 

combine DataFrame1 and DataFrame2 to DataFrame3

index value1 value2
1      one    <none>
2      two    <none>
3      three  just
4      four   just
5      five   just
6      six    done
7      seven  done

DataFrame3 has the same size of DataFrame1, and the match in value2 when index1>=index2. How can this be done in pandas?

Upvotes: 1

Views: 71

Answers (1)

Celius Stingher
Celius Stingher

Reputation: 18367

I am assuming in your data that index are actual indexes and not columns named "index". If so, you can solve this quickly with a join() and front filling. Otherwise you'd need to use merge, or set_index() previously:

Based on your dataset:

df_1.join(df_2).fillna(method='ffill')

Returns:

       value1 value2
index1              
1         one    NaN
2         two    NaN
3       three   just
4        four   just
5        five   just
6         six   done
7       seven   done

Full working example with set_index():

df_1 = pd.DataFrame({'index1':[1,2,3,4,5,6,7],
                     'value1':['one','two','three','four','five','six','seven']}).set_index('index1')
df_2 = pd.DataFrame({'index2':[3,6],
                     'value2':['just','done']}).set_index('index2')
print(df_1.join(df_2).fillna(method='ffill'))

Upvotes: 1

Related Questions