pandas split tuple column

Solutions on MaxInterview for pandas split tuple column by the best coders in the world

showing results for - "pandas split tuple column"
Bronwyn
28 Oct 2017
1In [2]: df = pd.DataFrame({'a':[1,2], 'b':[(1,2), (3,4)]})                                                                                                                      
2
3In [3]: df                                                                                                                                                                      
4Out[3]: 
5   a       b
60  1  (1, 2)
71  2  (3, 4)
8
9In [4]: df['b'].tolist()                                                                                                                                                        
10Out[4]: [(1, 2), (3, 4)]
11
12In [5]: pd.DataFrame(df['b'].tolist(), index=df.index)                                                                                                                                          
13Out[5]: 
14   0  1
150  1  2
161  3  4
17
18In [6]: df[['b1', 'b2']] = pd.DataFrame(df['b'].tolist(), index=df.index)                                                                                                                       
19
20In [7]: df                                                                                                                                                                      
21Out[7]: 
22   a       b  b1  b2
230  1  (1, 2)   1   2
241  2  (3, 4)   3   4
Hugo
15 Feb 2018
1In [2]: df = pd.DataFrame({'a':[1,2], 'b':[(1,2), (3,4)]})
2
3In [3]: df
4Out[3]:
5   a       b
60  1  (1, 2)
71  2  (3, 4)
8
9In [4]: df['b'].tolist()
10Out[4]: [(1, 2), (3, 4)]
11
12In [5]: pd.DataFrame(df['b'].tolist(), index=df.index)
13Out[5]:
14   0  1
150  1  2
161  3  4
17
18In [6]: df[['b1', 'b2']] = pd.DataFrame(df['b'].tolist(), index=df.index)
19
20In [7]: df
21Out[7]:
22   a       b  b1  b2
230  1  (1, 2)   1   2
241  2  (3, 4)   3   4
25