Good day,
I have a question, I don't know if someone can give me an idea how I can do it. I have the following DataFrame.
OBJECTID Coord_X Coord_Y
0 184839; 773866.1055; 9963245.606
1 186563; 773600.2571; 9963150.1
2 186254; 773465.1514; 9963101.804
3 188267; 773464.5314; 9963319.571
4 189278 773843.1493 9963128.459
Now what I need is to combine them together in such a way that they do not repeat themselves.
Example:
Combination of item 0 with 1
[(184839,{'Coord_X':773866.1055,'Coord_Y':9963245.606}), (186563,{'Coord_X':773600.2571,'Coord_Y':9963150.1})]
Combination of item 0 with 2
[(184839,{'Coord_X':773866.1055,'Coord_Y':9963245.606}), (186254,{'Coord_X':773465.1514,'Coord_Y':9963101.804})]
. . . .
Group combination of 2 of all items.
Thanks for your help.
I start by loading the data you give as an example. In your case, I suppose that you will read them from an excel, or from a CSV or similar, but here I am going to choose to read them directly from a string so that anyone can replicate my response without having to have the corresponding file:
This leaves us with
df
the following datframe which I assume is like the one you have:Solution
Answering your question, using
df.to_dict("index")
, pandas allows you to dump a dataframe as a dictionary whose keys are the indices of each row, and whose values are another dictionary with the rest of the data from that row (in this dictionary each key would be a column).This is close to what you are looking for. Just change the index to be "OBJECTID":
The resulting dictionary has a method
.items()
that displays it as a list of tuples, whose first element is the key and the second element is the value (the sub-dictionary in this case).Namely:
Once we have it this way, your question is equivalent to finding all the combinations of the elements of the previous list, taken 2 by 2.
The library
itertools
has the functioncombinations()
that serves precisely for this. It is enough to pass him the previous list and ask him for combinations of 2 by 2:And it comes out:
which I hope is what you were looking for.