比较两个csv并合并它们

时间:2017-06-20 06:34:38

标签: python angularjs csv pandas

我需要使用Python和Angular JS将一个CSV文件从python发送到网页并相应地显示它。

我有2个CSV文件,字段为

CSV1

Country code,Telecom,Latency

919618, India Private Mobile,0.008800

919619, India Private Mobile,0.008800

919620, India Private Mobile,0.008800

CSV2

Country code,Telecom,Latency

919802, India Private Mobile,0.008400

919803, India Private Mobile,0.008400

919620, India Private Mobile,0.008400

网页应显示为

Country code,Telecom,Latency

919618, India Private Mobile,0.008800

919619, India Private Mobile,0.008800

919802, India Private Mobile,0.008400

919803, India Private Mobile,0.008400

919620,India Private Mobile,0.008400,0.008800

(最后一行包含两个值,导致CSV文件包含919620个公共区域)

所以现在我是Angular JS和Flask的新手, 只是指导我如何开始?

从这个

开始
import pandas as pd
import numpy as np


dfa = pd.read_csv('rate4.csv', names=['Country Code', 'SP', 'Rate']);
dfb = pd.read_csv('rate3.csv', names=['Country Code', 'SP', 'Rate']);

1 个答案:

答案 0 :(得分:2)

您可以使用列DataFrameCountry code以及Telecomjoin的输出Latency使用concatgroupby

df = pd.concat([df1, df2]) \
       .groupby(['Country code','Telecom'])['Latency'] \
       .apply(lambda x: ','.join(x.astype(str))) \
       .reset_index()
print (df)
   Country code               Telecom        Latency
0        919618  India Private Mobile         0.0088
1        919619  India Private Mobile         0.0088
2        919620  India Private Mobile  0.0088,0.0084
3        919802  India Private Mobile         0.0084
4        919803  India Private Mobile         0.0084