这可能是一个简单/重复的问题,但我可以找到/想出如何做。在
我有两个csv文件:
信息.csv:
"Last Name", First Name, ID, phone, adress, age X [Total age: 100] |009076
abc, xyz, 1234, 982-128-0000, pqt,
bcd, uvw, 3124, 813-222-1111, tre,
poi, ccc, 9087, 123-45607890, weq,
然后呢
年龄.csv:
^{pr2}$我想根据中的“id
”列比较这两个csv文件信息.csv和“student_id
”来自年龄.csv取相应的“age_1
”数据并将其放入信息.csv。在
因此,最终输出应为:
信息.csv:
"Last Name", First Name, ID, phone, adress, age X [Total age: 100] |009076
abc, xyz, 1234, 982-128-0000, pqt,45
bcd, uvw, 3124, 813-222-1111, tre,20
poi, ccc, 9087, 123-45607890, weq,21
我可以简单地将基于键的表连接到新建.csv,但无法将数据放入列标题“age
”。我用“csvkit
”来做这个。在
我用的是:
csvjoin -c 3,1 info.csv age.csv > new.csv
您可以使用}。通过将两个数据帧的索引分别设置为},然后更新{}中的age列。之后,重新设置索引,使
Pandas
并使用age
数据更新{ID
和{ID
再次成为列。在输出:
^{pr2}$试试这个。。。在
如果有什么不清楚的地方,请告诉我。我使用dict是因为如果你的文件非常大,它应该更快,因为你只需要在年龄.csv一次。在
也许有一种更简单的方法/一些已经实现的东西…但这应该能做到。在
相关问题 更多 >
编程相关推荐