2017-04-17 44 views
1
import pandas as pd 
df = pd.DataFrame({ 
    'id':[1,2,3,4,5,6,7,8,9,10,11], 
    'text': ['abc','zxc','qwe','asf','efe','ert','poi','wer','eer','poy','wqr']}) 

我有列的數據幀:Python的大熊貓:追加數據幀的行和刪除附加行

id text 
1  abc 
2  zxc 
3  qwe 
4  asf 
5  efe 
6  ert 
7  poi 
8  wer 
9  eer 
10  poy 
11  wqr 

我有一個包含ID的列表清單L = [1,3,6,10]

我想從列表中追加文本列,從我的列表中第一次取1和3(列表中的前兩個值),並在我的DataFrame中追加帶有id = 1(其id爲2的文本列),然後刪除行id列2類似然後採取3和6,然後追加文本列其中id = 4,5爲id 3,然後刪除id = 4和5行,迭代爲列表中的元素(x,x + 1)

我的最後輸出看起來是這樣的:

id text 
1 abczxc   # joining id 1 and 2 
3 qweasfefe  # joining id 3,4 and 5 
6 ertpoiwereer # joining id 6,7,8,9 
10 poywqr   # joining id 10 and 11 

回答

3

您可以使用isincumsum的系列,這與applyjoin功能使用了groupby

s = df.id.where(df.id.isin(L)).ffill().astype(int) 
df1 = df.groupby(s)['text'].apply(''.join).reset_index() 
print (df1) 
    id   text 
0 1  abczxc 
1 3  qweasfefe 
2 6 ertpoiwereer 
3 10  poywqr 

這工作,因爲:

s = df.id.where(df.id.isin(L)).ffill().astype(int) 
print (s) 
0  1 
1  1 
2  3 
3  3 
4  3 
5  6 
6  6 
7  6 
8  6 
9  10 
10 10 
Name: id, dtype: int32 
+0

先生您先前的代碼'df.groupby(df.id.isin(L).cumsum())['text']。apply(''。join).reset_index()。rename(columns = {0:'text '})'工作正常,你爲什麼介紹'.ffill()。astype(int)'?我的意思是它是做什麼的? – Shubham

+0

它在id列[這裏](http://stackoverflow.com/posts/43454862/revisions)中有不同的輸出,所以我改變它。 – jezrael

1

使用pd.cut創建你的箱子然後帶有一個lambda函數的groupby可加入該組中的文本。

df.groupby(pd.cut(df.id,L+[np.inf],right=False, labels=[i for i in L])).apply(lambda x: ''.join(x.text)) 

編輯:

(df.groupby(pd.cut(df.id,L+[np.inf], 
       right=False, 
       labels=[i for i in L])) 
    .apply(lambda x: ''.join(x.text)).reset_index().rename(columns={0:'text'})) 

輸出:

id   text 
0 1  abczxc 
1 3  qweasfefe 
2 6 ertpoiwereer 
3 10  poywqr 
+0

在你的代碼的最後我添加.reset_index()將其轉換成數據幀,但它給了我命名爲0科拉姆,而不是'text' – Shubham

+0

@SRingne請參閱編輯。 –

3

我改變了值不在列表中ffill和GROUPBY np.nan然後。儘管@ Jezrael的方法好得多。我需要記住使用cumsum :)

l = [1,3,6,10] 
df.id[~df.id.isin(l)] = np.nan 
df = df.ffill().groupby('id').sum() 

     text 
id 
1.0  abczxc 
3.0  qweasfefe 
6.0  ertpoiwereer 
10.0 poywqr