#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pandas as pd
df1 = pd.read_csv('1.txt', sep=r'\s+', header=None, names=['a','b'])
print(df1)
df2 = pd.read_csv('2.txt', sep=r'\s+', header=None, names=['a','b','val'])
print(df2)
# 情况一,则 不变
df_s =[]
df_d =[]
df_s = df2[ (df1.a==df2.a) & (df1.b==df2.b)]
# 情况二,则调换位置
df_d = df2[ (df1.a==df2.b) & (df1.b==df2.a)][['b', 'a', 'val']]
df_d.columns = ['a', 'b', 'val']
res = pd.concat([df_s, df_d])
print("----------------\n:\n", res)
res.to_excel('result_data.xlsx', index=False)
结果: a b
0 184hefo|add.HEFO.ctg0001.559 184hefo|add.HEFO.ctg0002.26
1 184hefo|HEFO.ctg0001.951 184hefo|HEFO.ctg0002.67
2 184hefo|HEFO.ctg0001.953 184hefo|add.HEFO.ctg0002.27
3 184hefo|add.HEFO.ctg0002.28 184hefo|add.HEFO.ctg0001.566
4 184hefo|add.HEFO.ctg0001.576 184hefo|HEFO.ctg0002.70
a b val
0 184hefo|add.HEFO.ctg0001.559 184hefo|add.HEFO.ctg0002.26 1.39753
1 184hefo|HEFO.ctg0001.951 184hefo|HEFO.ctg0002.67 1.64152
2 184hefo|HEFO.ctg0001.953 184hefo|add.HEFO.ctg0002.27 1.67177
3 184hefo|add.HEFO.ctg0001.566 184hefo|add.HEFO.ctg0002.28 1.73668
4 184hefo|HEFO.ctg0002.70 184hefo|add.HEFO.ctg0001.576 2.26327
----------------
:
a b val
0 184hefo|add.HEFO.ctg0001.559 184hefo|add.HEFO.ctg0002.26 1.39753
1 184hefo|HEFO.ctg0001.951 184hefo|HEFO.ctg0002.67 1.64152
2 184hefo|HEFO.ctg0001.953 184hefo|add.HEFO.ctg0002.27 1.67177
3 184hefo|add.HEFO.ctg0002.28 184hefo|add.HEFO.ctg0001.566 1.73668
4 184hefo|add.HEFO.ctg0001.576 184hefo|HEFO.ctg0002.70 2.26327
可以了,再次感谢帮助,我会努力学习代码的 |