Use python to manipulate multiple csv data

Asked 2 years ago, Updated 2 years ago, 91 views

I would like to use python to put the two csv files together.

istlist1 br
route_node,X,Y
50775,-7882.7599,-43837.4058
50774,-7886.58284,-43851.12686 50772,-7895.8552,-43835.9677

istlist2 br
id,x,y
57136, -10171.54496, -44468.08496 31001,-10798.00999,-47051.58


when [list1] and [list2] are present as above. Consolidate the list into one

[new_list]
route_node, X, Y, id, x, y
50775, -7882.7599, -43837.4058, 57136, -10171.54496, -44468.08496 50775,-7882.7599,-43837.4058, 31001,-10798.00999,-47051.58
50774, -7886.58284, -43851.12686, 57136, -10171.54496, -44468.08496 50774,-7886.58284,-43851.12686, 31001,-10798.00999,-47051.58
·
·
·
To match all route_nodes in [list2] to all ids in [list1], such as
What functions should I use?
"I was thinking about using ""merge"", but I understand that ""merge"" is used when the two data have a common value, so I think ""merge"" is not available in this case."

I apologize for the rudimentary content, but I would appreciate it if you could teach me.
Thank you for your cooperation.

python pandas numpy csv

2022-09-30 21:44

2 Answers

Temporarily create a Common Data Value (hereafter in the key column) and join outside.

>>import pandas as pd
>>>df1=pd.read_csv('list1.csv')
>>>df2=pd.read_csv('list2.csv') 
>>df1.assign(key=0).merge(df2.assign(key=0), how='outer').\
    drop(columns='key') .to_csv('new_list.csv', index=False)

$ cat new_list.csv
route_node, X, Y, id, x, y
50775,-7882.7599,-43837.4058,57136,-10171.54496,-44468.08496
50775,-7882.7599,-43837.4058,31001,-10798.00999,-47051.58
50774,-7886.582840000001,-43851.126860000004,57136,-10171.54496,-44468.08496
50774,-7886.582840000001,-43851.126860000004,31001,-10798.00999,-47051.58
50772,-7895.8552,-43835.9677,57136,-10171.54496,-44468.08496
50772,-7895.8552,-43835.9677,31001,-10798.00999,-47051.58


2022-09-30 21:44

I think I can do the main thing with this itertools.product().
Itertools.product
that generates direct product (decart product) of multiple lists in Python itertools.product-return list installed of table

Input/output is csv so it's troublesome.Also, use the list processing around here.
5.1.About list types a little more
How to Flatten with Python 3

import csv
import itertools

list1 = [ ]
with open('list1.csv', 'r', newline=') asf:
    reader=csv.reader(f)
    For row in reader:
        list1.append(row)

list2 = [ ]
with open('list2.csv', 'r', newline=') asf:
    reader=csv.reader(f)
    For row in reader:
        list2.append(row)

header=list1.pop(0)
header.extend(list2.pop(0))

list3 = [sum(list(tup), []) for setup in intertools.product(list1, list2)]
list3.insert(0,header)

with open('list3.csv', 'w', newline=') asf:
    writer=csv.writer(f)
    writer.writerows (list 3)


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.