pandas 将两数据表进行拼接

将两文本内容以下图为例合并,空值补0: 

pandas 将两数据表进行拼接_第1张图片

 

import pandas as pd

def go(path1, path2, new_file_name):
    df1 = pd.read_csv(path1, encoding="utf-8")
    df2 = pd.read_csv(path2, encoding="utf-8")
    columns = df1.columns
    df_obj = pd.concat([df1,df2], sort=False)
    new_df = df_obj[columns].fillna(0)
    new_df.to_csv("{0}.csv".format(new_file_name), index=False, encoding='utf_8_sig')


if __name__ == '__main__':
    path1 = "动漫1.csv"
    path2 = "动漫2.csv"
    new_file_name = "动漫"
    go(path1, path2, new_file_name)

 

你可能感兴趣的:(Python,pandas,data_science)