2016-10-01 66 views
1

我用下面的代碼:如何在Python中分割數據框中的數據?

import pandas as pd 
pandas_bigram = pd.DataFrame(bigram_data) 
print pandas_bigram 

我得到的輸出如下

       0 
0      ashoka -**0 
1     - wikipedia,**1 
2    wikipedia, the**2 
3      the free**2 
4   free encyclopedia**2 
5   encyclopedia ashoka**1 
6     ashoka from**2 
7    from wikipedia,**1 
8    wikipedia, the**2 
9      the free**2 
10   free encyclopedia**2 

我的問題是如何分割該數據幀。所以,我會得到兩行數據。這裏的數據由「**」分隔。

+0

您是否正在閱讀csv文件的bigram_data? –

回答

0
import pandas as pd 

df= [" ashoka -**0","- wikipedia,**1","wikipedia, the**2"] 
df=pd.DataFrame(df) 

print(df) 
        0 
0  ashoka -**0 
1 - wikipedia,**1 
2 wikipedia, the**2 

使用split功能:該方法拆分()返回的字符串中的所有字的列表,使用str作爲分離器(上如果未指定所有空白分裂),任選限制性分裂爲num的數量。

df1 = pd.DataFrame(df[0].str.split('*',1).tolist(), 
             columns = ['0','1']) 

print(df1) 

       0 1 
0  ashoka - *0 
1 - wikipedia, *1 
2 wikipedia, the *2