Python 将给定行移动到数据帧的末尾
我希望从数据帧中获取给定的行,并将其前置或追加到同一数据帧 我下面的代码就是这样做的,但我不确定我是用正确的方法还是有更简单、更好、更快的方法Python 将给定行移动到数据帧的末尾,python,pandas,dataframe,concat,Python,Pandas,Dataframe,Concat,我希望从数据帧中获取给定的行,并将其前置或追加到同一数据帧 我下面的代码就是这样做的,但我不确定我是用正确的方法还是有更简单、更好、更快的方法 testdf = df.copy() #get row target_row = testdf.ix[[2],:] #del row from df testdf.drop([testdf.index[2]], axis=0, inplace=True) #concat original row to end or start of df newdf
testdf = df.copy()
#get row
target_row = testdf.ix[[2],:]
#del row from df
testdf.drop([testdf.index[2]], axis=0, inplace=True)
#concat original row to end or start of df
newdf = pd.concat([testdf, target_row], axis=0)
谢谢我可以把它简化为一行:
pd.concat([df.ix[0:1], df.ix[3:], df.ix[[2]]])
不过,我看不出您的代码和我的代码之间有任何性能差异。据推测,复制是最大的罪魁祸首。而不是concat,我会在ing后直接分配给df,然后使用
iloc
引用要分配行的位置,您必须调用挤压
,以便只分配值并丢失原始索引值,否则将引发值错误
:
In [210]:
df = pd.DataFrame({'a':np.arange(5)})
df
Out[210]:
a
0 0
1 1
2 2
3 3
4 4
In [206]:
target_row = df.ix[[2],:]
target_row
Out[206]:
a
2 2
In [211]:
df = df.shift()
df.iloc[0] = target_row.squeeze()
df
Out[211]:
a
0 2
1 0
2 1
3 2
4 3
编辑
要在末尾插入:
In [255]:
df = pd.DataFrame({'a':np.arange(5)})
target_row = df.ix[[2],:]
df = df.shift(-1)
df.iloc[-1] = target_row.squeeze()
df
Out[255]:
a
0 1
1 2
2 3
3 4
4 2
另一次更新
感谢@AsheKetchum指出我之前的答案是错误的,现在看看这三年后的情况,我意识到你可以reindex
orig df:
如果我们将索引的副本作为列表
:
In[24]:
idx = df.index.tolist()
idx
Out[24]: [0, 1, 2, 3, 4]
然后我们可以pop
从该列表中选择感兴趣的索引:
In[25]:
idx.pop(2)
idx
Out[25]: [0, 1, 3, 4]
In[26]:
df.reindex([2] + idx)
Out[26]:
a
2 2
0 0
1 1
3 3
4 4
现在,我们可以通过在列表前面加上前缀来重新编制索引:
In[25]:
idx.pop(2)
idx
Out[25]: [0, 1, 3, 4]
In[26]:
df.reindex([2] + idx)
Out[26]:
a
2 2
0 0
1 1
3 3
4 4
或附加:
In[27]:
df.reindex(idx+[2])
Out[27]:
a
0 0
1 1
3 3
4 4
2 2
为了提高性能,您可能需要考虑将要移动到的所有行的运行列表保持在数据文件的末尾,然后在单个<代码> P.CONTAT < /COD>操作中同时移动它们。
df = pd.DataFrame(np.random.rand(5, 3), columns=list('ABC'))
target_rows = [1, 3, 4]
a = df.iloc[[i for i in df.index if i not in target_rows], :]
b = df.iloc[target_rows, :]
>>> pd.concat([a, b])
A B C
0 0.818722 0.174153 0.522383
2 0.581577 0.840306 0.985089
1 0.645752 0.238476 0.670922
3 0.198271 0.501911 0.954477
4 0.965488 0.735559 0.701077
我只需删除一行并在末尾追加
df = pd.DataFrame({'a':np.arange(5)})
df.drop(2).append(df.ix[2]).reset_index(drop=True) # move 3rd row
df.drop(df.head(2).index).append(df.head(2)).reset_index() # move first 2 rows
您可能需要添加一些注释来解释您的答案。这是正确的吗??在编辑之前的示例中,您使
4
从列a
中消失。这是期望的输出吗?@AsheKetchum很好,再看看这个问题,OP做了什么我的答案是不正确的,我将更新说明.ix
不推荐使用。改用.iloc
。