Как объединить два фрейма данных в pandas?

#python #dataframe

#python #фрейм данных

Вопрос:

Существует два фрейма данных.

Как объединить поэлементно?

Вы можете увидеть код здесь

 df1 = pd.DataFrame(columns = ['string1', 'string2'])
df1.loc[len(df1), :] = ['Hello', 'This is Sam']
df1.loc[len(df1), :] = ['Good?', 'Are you free']

df2 = pd.DataFrame(columns = ['string1', 'string2'])
df2.loc[len(df2), :] = ['how are you?', 'from Canada']
df2.loc[len(df2), :] = ['morning', 'to have a talk?']

df1

  string1       string2
0   Hello   This is Sam
1   Good?  Are you free

df2

        string1          string2
0  how are you?      from Canada
1       morning  to have a talk?


#How to get the desired dataframe: [['Hello how are you?', 'This is Sam from Canada'], ['Good morning?', 'Are you free to have a talk?']]
  

Комментарии:

1. «Как получить следующую строку:»… Я вижу две строки? Не 1. И куда девается результат?

Ответ №1:

Если индекс и столбцы совпадают, используйте любую из следующих операций конкатенации строк фрейма данных.

 df1   ' '   df2

              string1                       string2
0  Hello how are you?       This is Sam from Canada
1       Good? morning  Are you free to have a talk?
  

 df1.add(' ').add(df2)

              string1                       string2
0  Hello how are you?       This is Sam from Canada
1       Good? morning  Are you free to have a talk?
  

 df2.radd(df1.add(' '))

              string1                       string2
0  Hello how are you?       This is Sam from Canada
1       Good? morning  Are you free to have a talk?