Python 用pandas编写文件比较
我比较两个文件中的列,如果文件1 col1中的值==文件2 col2中的值,则将结果写入第三个文件。我想用熊猫来代替 我的代码:Python 用pandas编写文件比较,python,pandas,Python,Pandas,我比较两个文件中的列,如果文件1 col1中的值==文件2 col2中的值,则将结果写入第三个文件。我想用熊猫来代替 我的代码: f1 = open( 'f1.txt', 'r') f2 = open( 'f2.txt', 'r') f3 = open( 'f3.txt', 'w+') for row1 in f1: f1_row = row1.strip('\n').split(' ') for row2 in f2: f2_row = row2.strip('
f1 = open( 'f1.txt', 'r')
f2 = open( 'f2.txt', 'r')
f3 = open( 'f3.txt', 'w+')
for row1 in f1:
f1_row = row1.strip('\n').split(' ')
for row2 in f2:
f2_row = row2.strip('\n').split(' ')
if f1_row[0] == f2_row[0]
f3.write('\t'.join(f2_row)
f2.seek(0,0)
f1.close()
f2.close()
f3.close()
示例文件和结果
file 1
1 3 5 6
3 8 9 0
4 5 7 1
2 6 7 9
file 2
0 5 6 7 9 9 1
3 4 5 6 7 7 4
2 8 7 9 3 2 0
1 0 3 3 4 6 7
7 8 3 9 1 1 2
result
1 0 3 3 4 6 7
3 4 5 6 7 7 4
2 8 7 9 3 2 0
我如何在熊猫身上产生这种效果?谢谢你在
pandas
df1=pd.read_csv('file1.csv')
df2=pd.read_csv('file2.csv')
result=df1[[1]].merge(df2,how='inner')
Out[236]:
1 2 3 4 5 6 7
0 1 0 3 3 4 6 7
1 3 4 5 6 7 7 4
2 2 8 7 9 3 2 0
result.to_csv('fil3.csv')
详细说明什么?我现在所做的工作很有效,但我想用另一种方式来写这篇文章,使用pandas似乎是最好的方式来处理如何让返回的行按照文件1的第1列中的顺序进行,就像我的结果一样。这似乎遵循了文件2中的顺序