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 :: Python program to draw hexagon 
Python :: find the highest id in model django 
Python :: python find digits in string 
Python :: python print value and variable name 
Python :: python array usage 
Python :: pandas legend placement 
Python :: python subtract every element in list 
Python :: shift list python 
Python :: python array index range 
Python :: add two numbers in python 
Python :: How to create role discord.py 
Python :: copy files to a directory using text file 
Python :: Python NumPy swapaxis Function Example 
Python :: django messages 
Python :: how to square root in python 
Python :: django-sslserver 
Python :: gspread_pandas pypi 
Python :: how to add percentage in countplot 
Python :: integer xticks 
Python :: how to create an entry box on tkinter python 
Python :: 3d array into 2d array python 
Python :: python last n array elements 
Python :: discord.py get server id 
Python :: python get desktop environment 
Python :: how to use input in python 
Python :: reading binary file 
Python :: select pandas by t dtype python 
Python :: df empty python 
Python :: import yaml python3 
Python :: display keys in a dictionary python 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =