|
@@ -0,0 +1,33 @@
|
|
|
+import pandas as pd
|
|
|
+
|
|
|
+
|
|
|
+df1 = pd.read_excel(r'D:\guozhong\来安县\来安县\9、来安县\来安县表层数据统计.xlsx', converters={'原样品编号': str})
|
|
|
+df2 = pd.read_excel(r'D:\guozhong\来安县\来安县\9、来安县\来安县土壤容重.xlsx')
|
|
|
+
|
|
|
+print(df1)
|
|
|
+
|
|
|
+df1['原样品编号'] = df1['原样品编号'].astype(str)
|
|
|
+df2['样品编号'] = df2['样品编号'].astype(str)
|
|
|
+
|
|
|
+
|
|
|
+df1['Processed ID'] = df1['原样品编号'].str[:-2]
|
|
|
+df2['Processed ID'] = df2['样品编号'].str[:-2]
|
|
|
+print(df1)
|
|
|
+print(df2)
|
|
|
+
|
|
|
+
|
|
|
+result_df = pd.DataFrame(columns=df2.columns)
|
|
|
+
|
|
|
+
|
|
|
+for id in df1['Processed ID']:
|
|
|
+
|
|
|
+ matched_row = df2[df2['Processed ID'] == id]
|
|
|
+
|
|
|
+ if not matched_row.empty:
|
|
|
+ result_df = pd.concat([result_df, matched_row], ignore_index=True)
|
|
|
+ else:
|
|
|
+
|
|
|
+ empty_row = pd.Series([''] * len(df2.columns), index=df2.columns)
|
|
|
+ result_df = pd.concat([result_df, empty_row.to_frame().T], ignore_index=True)
|
|
|
+
|
|
|
+result_df.to_excel('来安容重.xlsx', index=False)
|