我正在尝试添加两个数据帧,但没有得到正确的结果。我有两个文件,其中一个文件具有列名,另一个文件具有数据。我要合并它们。
我正在使用'\ 001'分隔符。
示例:
df1:
56447MNEMILY 2703546.742893.9553218262930LP2018-11-21 09:18:46.040618
62872ILOPDYKE 1708138.269688.8052618165922LP2018-11-21 09:18:46.040618
04925MECARATUNK 2302545.231369.9861207221305LP2018-11-21 09:18:46.040618
df2:
meli_zip_cd_basemeli_stt_provncdmeli_city_nmmeli_typmeli_cntry_fipsmeli_latimeli_longimeli_area_cdmeli_fin_cdmeli_last_lnmeli_facmeli_msa_cdmeli_pmsa_cdmeli_dma_cdload_dt
预期的最终结果:
df_final:
meli_zip_cd_basemeli_stt_provncdmeli_city_nmmeli_typmeli_cntry_fipsmeli_latimeli_longimeli_area_cdmeli_fin_cdmeli_last_lnmeli_facmeli_msa_cdmeli_pmsa_cdmeli_dma_cdload_dt
56447MNEMILY 2703546.742893.9553218262930LP2018-11-21 09:18:46.040618
62872ILOPDYKE 1708138.269688.8052618165922LP2018-11-21 09:18:46.040618
04925MECARATUNK 2302545.231369.9861207221305LP2018-11-21 09:18:46.040618
答案 0 :(得分:0)
如果我对您的理解正确,那么您希望df2
中的第一行(也是唯一行)成为df1
中第一行(也是唯一行)的标题:
df1.columns = df2.iloc[0].values
答案 1 :(得分:0)
我想我找到了解决方法:
df1 = pd.read_csv('/medaff/eureka/CDP/DMN_MELI_ZIP/DMN_MELI_ZIP.txt', delimiter='\001')
df2 = pd.read_csv('/medaff/eureka/CDP/HEADERS/DMN_MELI_ZIP_HEADER.txt', delimiter='\001')
df1.columns = df2.columns
df1.to_csv('/medaff/eureka/CDP/HEADERS/test.txt', sep ='\001', index=False)