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