Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas split column with tuple

In [2]: df = pd.DataFrame({'a':[1,2], 'b':[(1,2), (3,4)]})                                                                                                                      

In [3]: df                                                                                                                                                                      
Out[3]: 
   a       b
0  1  (1, 2)
1  2  (3, 4)

In [4]: df['b'].tolist()                                                                                                                                                        
Out[4]: [(1, 2), (3, 4)]

In [5]: pd.DataFrame(df['b'].tolist(), index=df.index)                                                                                                                                          
Out[5]: 
   0  1
0  1  2
1  3  4

In [6]: df[['b1', 'b2']] = pd.DataFrame(df['b'].tolist(), index=df.index)                                                                                                                       

In [7]: df                                                                                                                                                                      
Out[7]: 
   a       b  b1  b2
0  1  (1, 2)   1   2
1  2  (3, 4)   3   4
Comment

pandas split tuple column

In [2]: df = pd.DataFrame({'a':[1,2], 'b':[(1,2), (3,4)]})

In [3]: df
Out[3]:
   a       b
0  1  (1, 2)
1  2  (3, 4)

In [4]: df['b'].tolist()
Out[4]: [(1, 2), (3, 4)]

In [5]: pd.DataFrame(df['b'].tolist(), index=df.index)
Out[5]:
   0  1
0  1  2
1  3  4

In [6]: df[['b1', 'b2']] = pd.DataFrame(df['b'].tolist(), index=df.index)

In [7]: df
Out[7]:
   a       b  b1  b2
0  1  (1, 2)   1   2
1  2  (3, 4)   3   4
Comment

PREVIOUS NEXT
Code Example
Python :: factorial program in python 
Python :: pyttsx3 
Python :: stack in python 
Python :: list to dic 
Python :: django save object in view 
Python :: calculate the shortest path of a graph in python 
Python :: dataframe python 
Python :: python 
Python :: django background_task 
Python :: How to count the occurrence of certain item in an ndarray? 
Python :: plot matrix as heatmap 
Python :: random integer 
Python :: django changing boolean field from view 
Python :: q fields django Q objects 
Python :: fillna spark dataframe 
Python :: SUMOFPROD1 codechef solution 
Python :: how to print 2 list in python as table 
Python :: django form field add attrs 
Python :: pyqt5 drop down menu 
Python :: decision tree python 
Python :: how to convert string to float in python 
Python :: python pandas read_csv tsv 
Python :: length of list without len function 
Python :: adding strings together in python 
Python :: python typing list of specific values 
Python :: re.search() 
Python :: python how to delete a variable 
Python :: cosine similarity python 
Python :: python linux script 
Python :: python draw tree 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =