2016-11-11 29 views
3

我有以下数据框:如何将多维数据帧压缩到单个列中?

0 1 2 3 4 5 6 7 8 
0 Twitter (True 01/21/2015) None None None None None None None None 
1 Google, Inc. (True 11/07/2016) None None None None None None None None 
2 Microsoft, (True 07/01/2016) Facebook (True 11/01/2016) None None None None None None None 
3 standard & poors, Inc. (True 11/08/2016) None None None None None None None None 
8 apple (True 11/10/2016)  apple (True 11/01/2016)  None None None None None apple (True 11/01/2016)  None 

我如何可以压缩上述数据帧到一个单一的数据帧?:

0 
0 Twitter (True 01/21/2015) 
1 Google, Inc. (True 11/07/2016) 
2 Microsoft, (True 07/01/2016) \ Facebook (True 11/01/2016) 
3 standard & poors, Inc. (True 11/08/2016) \ 
8 apple (True 11/10/2016) \ apple (True 11/01/2016) \ apple (True 11/01/2016) 

我想:

df = df.iloc[:,0].join('\') 

不过,我不明白如何添加分隔符。我应该如何使用分隔符来压缩数据帧?

回答

3

我想你需要replaceNoneNaN然后stack删除NaN,最后groupbyapplyjoin

df = df.replace({None: np.nan, 'None': np.nan}).stack() 
df = df.groupby(level=0).apply(' \\ '.join) 
print (df) 
0       Twitter (True 01/21/2015) 
1      Google, Inc. (True 11/07/2016) 
2 Microsoft, (True 07/01/2016) \ Facebook (True ... 
3    standard & poors, Inc. (True 11/08/2016) 
8 apple (True 11/10/2016) \ apple (True 11/01/20... 
dtype: object 

与列表理解另一种解决方案:

df = df.replace({None: np.nan, 'None': np.nan}) 
#python 3 use str, python 2 basestring 
df = df.apply(lambda x : ' \\ '.join([y for y in x if isinstance(y, str)]), axis=1) 

print (df) 
0       Twitter (True 01/21/2015) 
1      Google, Inc. (True 11/07/2016) 
2 Microsoft, (True 07/01/2016) \ Facebook (True ... 
3    standard & poors, Inc. (True 11/08/2016) 
8 apple (True 11/10/2016) \ apple (True 11/01/20... 
dtype: object 

时序

#[50000 rows x 9 columns] 
df = pd.concat([df]*10000).reset_index(drop=True) 

In [43]: %timeit (df.replace({None: np.nan, 'None': np.nan}).apply(lambda x : ''.join([y for y in x if isinstance(y, str)]), axis=1)) 
1 loop, best of 3: 820 ms per loop 

In [44]: %timeit (df.replace({None: np.nan, 'None': np.nan}).stack().groupby(level=0).apply(' \\ '.join)) 
1 loop, best of 3: 4.62 s per loop 
+0

谢谢,但我有一个奇怪的格式。每个角色都失败了。 – student

+0

哪个更快?...我注意到第一个解决方案需要一点点 – student

+1

在更新解决方案中看到时序 – jezrael

0

你可以试试这个(我得到一个小的数据帧这似乎确定了以下输出):

df = pd.DataFrame({'0':['Twitter (True 01/21/2015)', 'Google, Inc. (True 11/07/2016)', ' Microsoft, (True 07/01/2016)'], '1':[None, None, 'Facebook (True 11/01/2016)'], '2':[None, None, None]}) 
df = df.replace({None: ' ', 'None': ' '}) 
df.astype(str).apply(lambda x: '\\'.join(x), axis=1) 


0      Twitter (True 01/21/2015)\ \ 
1     Google, Inc. (True 11/07/2016)\ \ 
2  Microsoft, (True 07/01/2016)\Facebook (True ... 
dtype: object 
+0

谢谢我有一个奇怪的格式。所有的角色都被拆分了。 – student

+0

你的输出是怎样的? –

+0

'我喜欢我' – student

相关问题