pandas 将给定行移动到 DataFrame 的末尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30940773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:30:12  来源:igfitidea点击:

Move given row to end of DataFrame

pythonpandasdataframeconcat

提问by Boosted_d16

I would like to take a given row from a DataFrame and prepend or append to the same DataFrame.

我想从 DataFrame 中获取给定的行并添加或附加到同一个 DataFrame。

My code below does just that, but I'm not sure if I'm doing it the right way or if there is an easier, better, faster way?

我下面的代码就是这样做的,但我不确定我的做法是否正确,或者是否有更简单、更好、更快的方法?

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)

Thanks

谢谢

回答by EdChum

Rather than concat I would just assign directly to the df after shifting, then use ilocto reference the position you want to assign the row, you have to call squeezeso that you assign just the values and lose the original index value otherwise it'll raise a ValueError:

而不是 concat 我只是在shifting之后直接分配给 df ,然后使用iloc引用您要分配行的位置,您必须调用squeeze以便您只分配值并丢失原始索引值,否则它会引发ValueError

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

EDIT

编辑

To insert at the end:

在最后插入:

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

Another update

另一个更新

Thanks to @AsheKetchum for pointing out that my earlier answer is incorrect, now looking at this 3 years later I realise you could just reindexthe orig df:

感谢@AsheKetchum 指出我之前的答案是不正确的,现在看着这个 3 年后我意识到你可以只reindex使用原版 df:

If we take a copy of the index as a list:

如果我们将索引的副本作为 a list

In[24]:
idx = df.index.tolist()
idx

Out[24]: [0, 1, 2, 3, 4]

then we can popthe index of interest from this list:

然后我们可以pop从这个列表中找到感兴趣的索引:

In[25]:
idx.pop(2)
idx

Out[25]: [0, 1, 3, 4]

Now we can reindexby prepending to this list:

现在我们可以reindex通过添加到这个列表:

In[26]:
df.reindex([2] + idx)

Out[26]: 
   a
2  2
0  0
1  1
3  3
4  4

Or appending:

或附加:

In[27]:    
df.reindex(idx+[2])

Out[27]: 
   a
0  0
1  1
3  3
4  4
2  2

回答by Alexander

To improve performance, you may want to consider keeping a running list of all rows you want to move to the end of the DataFrame, and then move them all at once in a single pd.concatoperation.

为了提高性能,您可能需要考虑保留要移动到 DataFrame 末尾的所有行的运行列表,然后在单个pd.concat操作中一次移动它们。

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

回答by chrisaycock

I can reduce it to a one-liner:

我可以将其简化为单行:

pd.concat([df.ix[0:1], df.ix[3:], df.ix[[2]]])

I don't see any performance difference between your code and mine though. Presumably the copying is the biggest culprit.

不过,我没有看到您的代码和我的代码之间有任何性能差异。据推测,抄袭是最大的罪魁祸首。

回答by YH Wu

I'd just drop a row(s) and append at the end.

我只是删除一行并在最后追加。

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