1

I have a dataframe tdf. It has several columns, three of which are X,Y,Z.

I would like to gather each row, and pass the values of X,Y,Z to a function as a single tuple.

At first I tried this:

def format_tuple(x):
   print(x)
   return x

tmdf = tdf[['X', 'Y', 'Z']].applymap(format_tuple)

This code however treats each column 'X', 'Y', 'Z' individually as seen in the print(x) prints each columns' value individually, not as three columns converted to a single row tuple.

Then I thought, turn the values into a tuple like this but it doesn't work:

tmdf = tdf[['X', 'Y', 'Z']].apply(tuple, axis=1).applymap(format_tuple)
cs95
  • 379,657
  • 97
  • 704
  • 746
Ivan
  • 7,448
  • 14
  • 69
  • 134

1 Answers1

1

applymap is for elementwise transformations. For your requirement, use apply along the first axis:

def format_tuple(x):
   print(tuple(x.tolist()))
   return x

np.random.seed(0)
df = pd.DataFrame(np.random.randint(1, 100, (5, 3)), columns=list('XYZ'))
df

    X   Y   Z
0  45  48  65
1  68  68  10
2  84  22  37
3  88  71  89
4  89  13  59

df[['X', 'Y', 'Z']].apply(format_tuple, axis=1)
(45, 48, 65)
(45, 48, 65)
(68, 68, 10)
(84, 22, 37)
(88, 71, 89)
(89, 13, 59)

Note that the first group is duplicated for performance reasons.

cs95
  • 379,657
  • 97
  • 704
  • 746