Python 根据第一列时间相同 合并列名不一样的两个文件怎么合并?

表一:

| 时间 | | 监测因子1 | 监测因子2 |监测因子3 | 监测因子4 |监测因子5 | 监测因子6|

|2020年10月 | 1 | 2 | 3 | 4 | 5 | 6 |
| 2020年11月 | 1 | 2 | 3 | 4 | 5 | 6 |
| 2020年12月 | 1 | 2 | 3 | 4 | 5 | 6 |
| 2021年月1| | 1 | 2 | 3 | 4 | 5 | 6 |

表二:
| 时间 | | 监测因子1 | 监测因子2 |监测因子3 | 监测因子4 |监测因子7 | 监测因子8|

|2020年10月 | 1 | 2 | 3 | 4 | 7 | 8 |
| 2020年12月 | 1 | 2 | 3 | 4 | 7 | 8 |

最后合成结果:

| 时间 | | 监测因子1 | 监测因子2 |监测因子3 | 监测因子4 |监测因子5 | 监测因子6|监测因子7 | 监测因子8|

|2020年10月 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 2020年12月 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

先用merge合并,再使用条件判断进行选取,参考一下如下代码:

import pandas as pd
df1=pd.read_csv('t891.txt',sep=',')
#print(df1)
df2 = pd.read_csv('t892.txt', sep=',')
#print(df2)
df = pd.concat([df1, df2], axis=1, join="outer").reindex(df1.index)
#df=df1.join(df2,how='left',lsuffix='_l',rsuffix='_r').dropna().reset_index()
df = pd.merge(df1, df2, how='outer')
df=df[~df.isnull().any(axis=1)]
df.iloc[:, 1:]=df.iloc[:, 1:].astype(int)
print(df)
时间  监测因子1  监测因子2  监测因子3  监测因子4  监测因子5  监测因子6  监测因子7  监测因子8
0  2020年10月      1      2      3      4      5      6      7      8
2  2020年12月      1      2      3      4      5      6      7      8

如有帮助,请点一下我回答上方的采纳按钮。

分别读取两个文件,用字典存储