Home > Software design >  How to create tuples inside list of tuples - Python
How to create tuples inside list of tuples - Python

Time:10-06

My aim is to create a tuple inside another tuple, for example: (398905, 6340963, 2019) should become ((398905, 6340963), 2019)

I created a Pandas DataFrame which I converted into a list of tuples:

#create dataframe with 3 rows, 4 columns
data = {'X_coords':[398905, 492561, 496561], 'Y_coords':[6340963, 6526362, 6527362], 'YEAR': [2019, 2020, 2020], 'ID':[1,2,3]}
#to pandas dataframe
df = pd.DataFrame(data)

#creating a list of tuples from dataframe entries
df_tuple = [tuple(x) for x in df.values.tolist()]
#examine
print(df_tuple)

#prints:
[(398905, 6340963, 2019, 1), (492561, 6526362, 2020, 2), (496561, 6527362, 2020, 3)]

Considering my aim, the result should become: [((398905, 6340963), 2019, 1), ((492561, 6526362), 2020, 2), ((496561, 6527362), 2020, 3)]

What is a convenient way of achieving this?

CodePudding user response:

I think the easiest way is to do:

df_tuple = [((i[:2]),*i[2:]) for i in df.values.tolist()]

Basically saying you take the first items and put them in a tuple, then unpack the remaining items in the "larger" tuple.

CodePudding user response:

If you just want to group the first to elements of each tuple of the list, you could use a comprehension:

[((a, b),*c) for a, b, *c in df_tuples]

it gives as expected:

[((398905, 6340963), 2019, 1), ((492561, 6526362), 2020, 2), ((496561, 6527362), 2020, 3)]

CodePudding user response:

You could apply the required format and then convert tolist - i.e.

df.apply(lambda row: ((row['X_coords'], row['Y_coords']), row['YEAR'], row['ID']), axis=1).tolist()

Output

[((398905, 6340963), 2019, 1),
 ((492561, 6526362), 2020, 2),
 ((496561, 6527362), 2020, 3)]

CodePudding user response:

It is a one line solution which works directly with your data. Standard library is more than powerful for easy tasks

result = [((x, y), t, i) for x, y, t, i in zip(*[v for _, v in data.items()])]
  • Related