Home > database >  I want to combine two different size pandas.DataFrame
I want to combine two different size pandas.DataFrame

Time:08-06

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?

CodePudding user response:

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'))

CodePudding user response:

use append method

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'), index=['x', 'y'])

df
   A  B
x  1  2
y  3  4

df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'), index=['x', 'y'])
df.append(df2)

   A  B
x  1  2
y  3  4
x  5  6
y  7  8
  • Related