EXCEL处理|pandas使用方法记录

excel操作

读取excel

读取csv,excel文件,选择其中某些列,重命名列名,合并两个excel

former_file_path1 = "信息.csv"
former_file_path2 = "信息2.csv"
former_data1 = pd.read_csv(former_file_path1,usecols=["F015V_0007","标题","公告日期","公告地址","OB_ORGID_0045","代码"],encoding="utf-8")
former_data2=pd.read_csv(former_file_path2,encoding="utf-8")
former_data = pd.concat([former_data1,former_data2])

new_file_path = "20220523.xlsx"
new_data = pd.read_excel(new_file_path, usecols=["ORGNAME","F003V","PUBLISH_DATE","URL","ORGID"])
new_data=new_data.rename(columns={"ORGNAME":"公司名","F003V":"标题","PUBLISH_DATE":"公告日期"})

比较两个df数据-datacompy

import datacompy

compare = datacompy.Compare(new_data,former_data,join_columns=["标题"])

print(compare.report(sample_count=3000))
pd.DataFrame(compare.df2_unq_rows).to_excel("旧的有新的没有.xlsx",index=False)
pd.DataFrame(compare.df1_unq_rows).to_excel("新的有旧的没有.xlsx",index=False)


print("df1独有的行:")
print(compare.df1_unq_rows)
print("df2独有的行:")
print(compare.df2_unq_rows)
print("df1独有的列:")
print(compare.df1_unq_columns())
print("df2独有的列:")
printcompare.df2_unq_columns())
print("不匹配的行:")
print(compare.all_mismatch())
print("列名是否匹配")
print(compare.all_columns_match())
print("是否匹配")
print(compare.matches())

你可能感兴趣的:(python,机器学习,开发语言)