简体   繁体   中英

Concat, or merge two dataframe pandas

I have two dataframes here with shape of first dataframes are (10840, 109) and the second are (0,112)

I want to concat/merge two dataframes above. I tried df_part_2 = pd.concat([df_revisi_data,df_migrasi_part2],axis=1) , however the result was (10840, 221). I tried another way with df_part_2 = pd.concat([df_revisi_data,df_migrasi_part2],axis=0) but error rise InvalidIndexError: Reindexing only valid with uniquely valued Index objects

Honestly, all columns on the first dataframe are exist on the second dataframes. However, the result don't meet my expectation. The result of the new dataframe shape are (10840,112) with the data contain of the first dataframe, but the columns referes to the second dataframe.

slice of first dataframe

    No  Lead ID*      Nama Petani   Point   Tanggal Pengajuan*  Nama PIC Utama* Email PIC Utama*    Grading Point*  CF0 FUA pov - Status*   CF0 FUA pov - Start Date*   ... CF10 Verifikasi PKS - Email PIC*    CF10 Open Limit - Status*   CF10 Open Limit - Start Date*   CF10 Open Limit - End Date* CF10 Open Limit - Remarks   CF10 Open Limit - Nama PIC* CF10 Open Limit - Email PIC*    Review \nby Eng Migrated \nby Eng   Note \nby Eng
0   1   5147hvhaau26    AMAN WIJAYA LOMBOK TENGAH   2021-08-01 00:00:00 Ria Arumsari    ria.arumsari@efishery.com   Grade B APPROVED    2021-08-01 00:00:00 ... fitria.rahma@efishery.com   ACTIVE  2021-10-16 00:00:00 2021-10-16 00:00:00 STATUS DETAIL: REMARKS: Fitria Rahma Nur S  fitria.rahma@efishery.com   TRUE    TRUE    None
1   2   512bcr75iqp8    SAHMA DAHNIARTI BENGKULU SELATAN    2021-08-01 00:00:00 Fitria Rahma Nur S  fitria.rahma@efishery.com   Grade A APPROVED    2021-08-01 00:00:00 ... fitria.rahma@efishery.com   ACTIVE  2021-08-27 00:00:00 2021-08-27 00:00:00 STATUS DETAIL: Next Step - Open Access REMARKS: Fitria Rahma Nur S  fitria.rahma@efishery.com   TRUE    TRUE    None
2   3   5124qwzj2vxv    CAHYO SUBEKTI   SLEMAN  2021-08-02 00:00:00 Fitria Rahma Nur S  fitria.rahma@efishery.com   Grade B APPROVED

slice of second dataframe (actually only header of columns)

No  Kode Nominatif  Lead ID*    Nama Petani Point   Tanggal Pengajuan*  Nama PIC Utama* Email PIC Utama*    Requested Amount    Grading Point*  

the result I expected:

    No  Kode Nominatif  Lead ID*    Nama Petani Point   Tanggal Pengajuan*  Nama PIC Utama* Email PIC Utama*    Requested Amount    Grading Point*
    1    nan           5147hvhaau26 AMAN WIJAYA LOMBOK TENGAH   2021-08-01 00:00:00

Can you please check if this sample helps

df1 = pd.DataFrame({
    'col1' : [1,2,3,4,5],
    'col2' : [6,7,8,9,10],
    'col3': [11,12,13,14,15],
    'col4': [16,17,18,19,20]
    
})

df2 = pd.DataFrame(columns=["col3", "col4", "col5"])

Concat using

pd.concat([df1, df2], axis=0)

Result

col1    col2    col3    col4    col5
0   1.0 6.0 11  16  NaN
1   2.0 7.0 12  17  NaN
2   3.0 8.0 13  18  NaN
3   4.0 9.0 14  19  NaN
4   5.0 10.0    15  20  NaN

IIUC, your first df has values and the second df is empty. second df has extra columns where you want nan. Lemme know if my understanding is erroneous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM