Convert df column to a tuple
Asked Answered
C

3

6

I am having trouble converting a df column into a tuple that I can iterate through. I started with a simple code that works like this:

 set= 'pare-10040137', 'pare-10034330', 'pare-00022936', 'pare-10025987', 'pare-10036617'

 for i in set:

     ref_data=req_data[req_data['REQ_NUM']==i] 

This works fine, but now I want my set to come from a df. The df looks like this:

 open_reqs

Out[233]: 
          REQ_NUM
4825  pare-00023728
4826  pare-00023773 
.... ..............

I want all of those REQ_NUM values thrown into a tuple, so I tried to do open_reqs.apply(tuple, axis=1) and tuple(zip(open_reqs.columns,open_reqs.T.values.tolist())) but it's not able to iterate through either of these.

My old set looks like this, so this is the format I need to match to iterate through like I was before. I'm not sure if the Unicode is also an issue (when I print above I get (u'pare-10052173',)

 In[236]: set
 Out[236]: 
 ('pare-10040137',
  'pare-10034330',
  'pare-00022936',
  'pare-10025987',
  'pare-10036617')

So basically I need the magic code to get a nice simple set like that from the REQ_NUM column of my open_reqs table. Thank you!

Circuitry answered 16/5, 2016 at 18:45 Comment(0)
C
10

The following statement makes a list out of the specified column and then converts it to an array of tuple

open_req_list = tuple(list(open_reqs['REQ_NUM']))
Circuitry answered 17/5, 2016 at 22:19 Comment(0)
T
1

You can use the tolist() function to convert to a list and the tuple() the whole list

req_num = tuple(open_reqs['REQ_NUM'].tolist())
#type(req_num)
req_num
Thorncombe answered 18/2, 2022 at 13:13 Comment(0)
A
0
df = pd.DataFrame(data)

columns_tuple = tuple(df.columns)

df.columns has the datatype of object. To convert it into tuples, use this code and you will GET TUPLE OF ALL COLUMN NAMES

Avidin answered 9/2, 2023 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.