重新排列数据框列的R代码的Python/pandas等同于什么? [英] What would be Python/pandas equivalent of this R code for rearranging columns of a dataframe?
本文介绍了重新排列数据框列的R代码的Python/pandas等同于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
data<-data[c(8,1:7)]
基本上,将最后一列移到第一位置.我该如何在Python中通过仅使用列索引(最好在一行中)来做到这一点?
Basically, move the last column to the first position. How would I do this in Python by using only column indices, preferably in a single line?
推荐答案
您可以使用.iloc[]
:
data = data.iloc[:, [7] + list(range(7))]
或:data = data[[7] + list(range(7))]
请记住
-
python中的
- 索引是从零开始的,而R是从一开始的;
- R使用向量进行索引,而python使用列表.
- in python index is zero based while R is one based;
- R uses vector for indexing while python uses list.
这篇关于重新排列数据框列的R代码的Python/pandas等同于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文