Python 在熊猫数据框中移动列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35321812/
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
move column in pandas dataframe
提问by user308827
I have the following dataframe:
我有以下数据框:
a b x y
0 1 2 3 -1
1 2 4 6 -2
2 3 6 9 -3
3 4 8 12 -4
How can I move columns b and x such that they are the last 2 columns in the dataframe? I would like to specify b and x by name, but not the other columns.
如何移动 b 和 x 列,使它们成为数据框中的最后 2 列?我想按名称指定 b 和 x,但不是其他列。
采纳答案by Alexander
You can rearrange columns directly by specifying their order:
您可以通过指定它们的顺序直接重新排列列:
df = df[['a', 'y', 'b', 'x']]
In the case of larger dataframes where the column titles are dynamic, you can use a list comprehension to select every column not in your target set and then append the target set to the end.
在列标题是动态的较大数据框的情况下,您可以使用列表理解来选择不在目标集中的每一列,然后将目标集附加到末尾。
>>> df[[c for c in df if c not in ['b', 'x']]
+ ['b', 'x']]
a y b x
0 1 -1 2 3
1 2 -2 4 6
2 3 -3 6 9
3 4 -4 8 12
To make it more bullet proof, you can ensure that your target columns are indeed in the dataframe:
为了使其更加防弹,您可以确保您的目标列确实在数据框中:
cols_at_end = ['b', 'x']
df = df[[c for c in df if c not in cols_at_end]
+ [c for c in cols_at_end if c in df]]
回答by Charlie Haley
cols = list(df.columns.values) #Make a list of all of the columns in the df
cols.pop(cols.index('b')) #Remove b from list
cols.pop(cols.index('x')) #Remove x from list
df = df[cols+['b','x']] #Create new dataframe with columns in the order you want
回答by Roberto Williams Batista
You can use to way below. It's very simple, but similar to the good answer given by Charlie Haley.
您可以使用以下方式。这很简单,但类似于 Charlie Haley 给出的好答案。
df1 = df.pop('b') # remove column b and store it in df1
df2 = df.pop('x') # remove column x and store it in df2
df['b']=df1 # add b series as a 'new' column.
df['x']=df2 # add b series as a 'new' column.
Now you have your dataframe with the columns 'b' and 'x' in the end. You can see this video from OSPY : https://youtu.be/RlbO27N3Xg4
现在,您的数据框最后包含 'b' 和 'x' 列。你可以从 OSPY 看到这个视频:https://youtu.be/RlbO27N3Xg4
回答by hdinh
You can also do this as a one-liner:
您也可以单行执行此操作:
df.drop(columns=['b', 'x']).assign(b=df['b'], x=df['x'])
回答by jpp
You can use pd.Index.difference
with np.hstack
, then reindex
or use label-based indexing. In general, it's a good idea to avoid list comprehensions or other explicit loops with NumPy / Pandas objects.
您可以使用pd.Index.difference
with np.hstack
, thenreindex
或使用基于标签的索引。通常,避免使用 NumPy / Pandas 对象进行列表推导式或其他显式循环是个好主意。
cols_to_move = ['b', 'x']
new_cols = np.hstack((df.columns.difference(cols_to_move), cols_to_move))
# OPTION 1: reindex
df = df.reindex(columns=new_cols)
# OPTION 2: direct label-based indexing
df = df[new_cols]
# OPTION 3: loc label-based indexing
df = df.loc[:, new_cols]
print(df)
# a y b x
# 0 1 -1 2 3
# 1 2 -2 4 6
# 2 3 -3 6 9
# 3 4 -4 8 12
回答by TopGunner
This function will reorder your columns without losing data. Any omitted columns remain in the center of the data set:
此功能将重新排序您的列而不会丢失数据。任何省略的列都保留在数据集的中心:
def reorder_columns(columns, first_cols=[], last_cols=[], drop_cols=[]):
columns = list(set(columns) - set(first_cols))
columns = list(set(columns) - set(drop_cols))
columns = list(set(columns) - set(last_cols))
new_order = first_cols + columns + last_cols
return new_order
Example usage:
用法示例:
my_list = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
reorder_columns(my_list, first_cols=['fourth', 'third'], last_cols=['second'], drop_cols=['fifth'])
# Output:
['fourth', 'third', 'first', 'sixth', 'second']
To assign to your dataframe, use:
要分配给您的数据框,请使用:
my_list = df.columns.tolist()
reordered_cols = reorder_columns(my_list, first_cols=['fourth', 'third'], last_cols=['second'], drop_cols=['fifth'])
df = df[reordered_cols]
回答by SteveJ
An alternative, more generic method;
另一种更通用的方法;
from pandas import DataFrame
def move_columns(df: DataFrame, cols_to_move: list, new_index: int) -> DataFrame:
"""
This method re-arranges the columns in a dataframe to place the desired columns at the desired index.
ex Usage: df = move_columns(df, ['Rev'], 2)
:param df:
:param cols_to_move: The names of the columns to move. They must be a list
:param new_index: The 0-based location to place the columns.
:return: Return a dataframe with the columns re-arranged
"""
other = [c for c in df if c not in cols_to_move]
start = other[0:new_index]
end = other[new_index:]
return df[start + cols_to_move + end]
回答by Vrun
similar to Roberto Williams Battista's answer above, but hopefully a bit more robust:
类似于上面罗伯托·威廉姆斯·巴蒂斯塔的回答,但希望更强大一点:
df.insert(len(df.columns)-1, 'b', df.pop('b'))
df.insert(len(df.columns)-1, 'x', df.pop('x'))
回答by abhish
print(df) ['x','y','z','p','q'] #Our data frame are in this order
print(df) ['x','y','z','p','q'] #我们的数据框是这个顺序
moving z to second position, by simply modifying as below, observe z position.
将 z 移动到第二个位置,通过简单的修改如下,观察 z 位置。
df = df[['x','z','y','q','p']] df
df = df[['x','z','y','q','p']] df