0

I have a dataframe like this:

    top1    top2    top3
0   0       13      20
1   1       14      23
2   2       11      25
3   3       13      20
4   4       10      21
5   5       19      13

I want to generate to generate tuples like that: [("0", "13"), ("0", "20"), ("1", "14), ("2", "11"), ("2", "25"), ....].

How can we do that if possible? And if I want to do the same for a some given rows (not all the rows)?

bib
  • 944
  • 3
  • 15
  • 32
  • What you want is to iterate the row and make two tuples from each row right? – tbrugere Jun 03 '21 at 15:10
  • @Nephanth yes but always the combination is between the the first element of the row with each of the other columns – bib Jun 03 '21 at 15:11
  • Then do just that I suppose? Here is how to iterate through the rows: https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas – tbrugere Jun 03 '21 at 15:14
  • 1
    But remember that this way of handling the data isn't well suited to pandas. Why do you want to do this? – tbrugere Jun 03 '21 at 15:15
  • I want to generate tuple which represent edge in a graph – bib Jun 03 '21 at 15:16
  • Would it be ok to generate a dataframe (with two columns) instead of a list of tuples? That would be way more suited to pandas – tbrugere Jun 03 '21 at 15:18
  • You can do what do you want just I need the tuples to be like I want in order to generate my graph – bib Jun 03 '21 at 15:19

3 Answers3

0

assuming this is pandas we are talking about

import pandas as pd

df = pd.DataFrame({"top1": [0, 1, 2, 3, 4, 5], ... and so on})

tuples = []
for index, row in df.iterrows():
    tuples.append((row["top1"], row["top2"]))
    tuples.append((row["top1"], row["top3"]))

print(tuples)
Jakub Dóka
  • 2,477
  • 1
  • 7
  • 12
0

An alternative to iterating over the DataFrame would be to create two columns using zip and then convert them to two lists and join them. zip is a very efficient function for this.

df['col1'] = list(zip(df.top1, df.top2))
df['col2'] = list(zip(df.top1, df.top3))

final_list = df.col1.to_list() + df.col2.to_list()

If you want to get a list of row-wise tuples, you can do this:

def get_edge(idx):
    lst = []
    lst.append(df.col1[idx])
    lst.append(df.col2[idx])
    return lst

This returns a list of the pair of tuples whose index you pass to the function.

Shivam Roy
  • 1,961
  • 3
  • 10
  • 23
  • 1
    Can you please update your answer in case we need to select a given row in the dataframe – bib Jun 03 '21 at 15:37
  • Sure, you can do that by specifying the index. I've updated my answer and created a function to get a row-wise list of tuples, – Shivam Roy Jun 03 '21 at 15:45
0

Here are a couple of one-line solutions:

pd.concat([df[['top1', 'top2']].apply(tuple, axis=1),
           df[['top1', 'top3']].apply(tuple, axis=1)]).to_list()
(df[['top1', 'top2']].apply(tuple, axis=1)
                     .append(df[['top1', 'top3']].apply(tuple, axis=1))
                     .to_list())
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50