3

I work in economics and I'm a newbie in programming, so please pardon my lack of technical knowledge here.

Im using iPython 2.7 and I want to analyze a production system as network. Therefore Im trying to create a network out of a csv-file. The csv-file includes two columns. First column is a representation of work orders, the second represents working stations. The working stations have to be nodes of the network.

ORDER_ID;MACHINE_ID;
0;0;
0;1;
1;1;
2;2;
2;3;
2;4;
2;5;
2;6;
2;1;
2;7;
2;2;
3;8;
3;1;
4;9;
5;10;
5;10;
5;5;
5;11;
5;0; 
5;12;

As long as the order ID is the same number in different rows, the nodes of these rows have to be linked as edge. As soon as the Order ID changes, the next row has to checked and linked as edge. Edges are directed for better analysis. I wrote an algorithm, but I have problems with picking row item.

import networkx as nx
import csv 

g = nx.DiGraph()

with open("Data.csv") as dataSet:
    data = csv.reader()
    for OrderID1 in dataSet:
        OrderID = OrderID[0]
        for MachineID1 in dataSet:
            MachineID = MachineID1[1]
                if OrderID[0][i] == OrderID[0][i+1]:  
                    g.add_edge(MachineID[0][i],MachineID[0][i+1])
                elif OrderID[0][i] != row[0][i+1]: 
                    g.add_edge(row[0][i+1],row[0][i+2])
                else:
                    dataSet.close()

Result have to look like this:

g.edges()

>>> (0,1),(2,3),(3,4),(4,5),(5,6),(6,1),(1,7),(7,2),(8,1),(10,10),(10,5),(5,11),(11,0),(0,12)

Many thanks to everyone who takes time to respond!

1
  • I think in the line after the first for loop it should be OrderID = OrderID1[1]. Also, I believe you have a problem with indentation of the if condition after the second for loop so I can not tell what is its scope (it would give you an error for wrong indentation I suppose). I shall post the algorithm as I see it then you can compare it to what you intended to do. Commented Oct 12, 2015 at 16:08

1 Answer 1

2

The algorithm as I see it should be as follows:

import csv
import networkx as nx
g = nx.DiGraph()
data_csv = open('Data.csv')
csv_f = csv.reader(data_csv,delimiter=';')
prev_orderID = -1
prev_machineID = -1
for row in csv_f:
    if row[0] == prev_orderID:
        g.add_edge(prev_machineID,row[1])
    prev_orderID = row[0]
    prev_machineID = row[1]
print g.edges()

I have taken the sample you gave into a .csv file and the result was the same as your expectations:

[('11', '0'), ('10', '10'), ('10', '5'), ('1', '7'), ('0', '1'), ('0', '12'), ('3', '4'), ('2', '3'), ('5', '11'), ('5', '6'), ('4', '5'), ('7', '2'), ('6', '1'), ('8', '1')]

Maybe you had problems reading the rows because you did not specify the delimiter.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks you very much. It works!!!! You was right, to use the delimiter for opening the file. And thank you for your solution!!
Glad it works, you can tick the answer so if anyone had a similar problem can know that this answer worked for you. @DavidKos
I want to ask what can I do to make this representation, if I have a csv with multiple columns(like ORDER_ID,CUSTOMER_ID) and one target_label(which we can say MACHINE_ID in this case)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.