8

I have the following pandas dataframe df:

     Description    Code
0    Apples         014
1    Oranges        015
2    Bananas        017
3    Grapes         021

I need to convert it to a tuple of tuples, like this:

my_fruits = ( ('Apples', '014'), 
              ('Oranges', '015'), 
              ('Bananas', '017'), 
              ('Grapes', '021')
            )

Can you help me, please? I have tried the following code but it does not return what I really want:

list(zip(df.columns,df.T.values.tolist()))

Thanks in advance!!!

Brian
  • 2,163
  • 1
  • 14
  • 26
Gabriela M
  • 605
  • 1
  • 10
  • 25

3 Answers3

12

Would something like this work?

tuple(df.itertuples(index=False, name=None))
TheHCA
  • 336
  • 1
  • 8
  • I prefer this way because it is more generic and I do not need to hard code the column names. This should be the accepted answer. – nitin3685 Oct 09 '19 at 18:51
3

You need to zip the two columns:

tuple(zip(df.Description, df.Code))
# (('Apples', 14), ('Oranges', 15), ('Bananas', 17), ('Grapes', 21))
Psidom
  • 209,562
  • 33
  • 339
  • 356
  • Thank you!! It worked!! Do you know how I can remove the u sign before each string? it says u('Apples', u '014') – Gabriela M Aug 17 '18 at 14:50
  • 1
    It just means [your strings are in unicode](https://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string) – TheHCA Aug 17 '18 at 14:57
  • You probably are using python 2. As commented @TheHCA, it indicates the strings are unicode. You can use [this approach](https://stackoverflow.com/questions/1207457/convert-a-unicode-string-to-a-string-in-python-containing-extra-symbols) to convert unicode to ascii if you want them in ascii and don't mind losing some information on non-ascii characters. But generally you should be fine leaving the `u` alone. – Psidom Aug 17 '18 at 15:02
  • @Psidom, I understood your point. However, it always fails because a tuple does not have that attribute encode/decode :( – Gabriela M Aug 17 '18 at 15:04
  • You can't directly encode and decode a tuple; It has to be on the strings, something like `tuple((x.encode('ascii'), y.encode('ascii')) for x, y in zip(df.Description, df.Code))`. – Psidom Aug 17 '18 at 15:08
0

Though it is not recommended for large dataframes, you can also use apply

import pandas as pd
df = pd.DataFrame([['Apples', '014'], 
                   ['Oranges', '015'],
                   ['Bananas', '017'],
                   ['Grapes', '021']], columns = ['Fruit', 'Value'])

my_fruits = tuple(df[['Fruit','Value']].apply(tuple, axis=1))
my_fruits

Giving:

(('Apples', '014'), ('Oranges', '015'), ('Bananas', '017'), ('Grapes', '021'))

Use df.columns instead of explicitly typing out the column names if you want it to be more generic.

R Walser
  • 330
  • 3
  • 16