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
先生您先前的代碼'df.groupby(df.id.isin(L).cumsum())['text']。apply(''。join).reset_index()。rename(columns = {0:'text '})'工作正常,你爲什麼介紹'.ffill()。astype(int)'?我的意思是它是做什麼的? – Shubham
它在id列[這裏](http://stackoverflow.com/posts/43454862/revisions)中有不同的輸出,所以我改變它。 – jezrael