我有以下DataFrame(df):
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(10, 5))
我通过分配添加更多列:
df['mean'] = df.mean(1)
如何将列的意思移到前面,即将其设置为第一列,而其他列的顺序保持不变?
我有以下DataFrame(df):
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(10, 5))
我通过分配添加更多列:
df['mean'] = df.mean(1)
如何将列的意思移到前面,即将其设置为第一列,而其他列的顺序保持不变?
当前回答
import numpy as np
import pandas as pd
df = pd.DataFrame()
column_names = ['x','y','z','mean']
for col in column_names:
df[col] = np.random.randint(0,100, size=10000)
您可以尝试以下解决方案:
解决方案1:
df = df[ ['mean'] + [ col for col in df.columns if col != 'mean' ] ]
解决方案2:
df = df[['mean', 'x', 'y', 'z']]
解决方案3:
col = df.pop("mean")
df = df.insert(0, col.name, col)
解决方案4:
df.set_index(df.columns[-1], inplace=True)
df.reset_index(inplace=True)
解决方案5:
cols = list(df)
cols = [cols[-1]] + cols[:-1]
df = df[cols]
解决方案6:
order = [1,2,3,0] # setting column's order
df = df[[df.columns[i] for i in order]]
时间比较:
解决方案1:
CPU时间:用户1.05 ms,sys:35µs,总计:1.08 ms壁时间:995µs
解决方案2:
CPU时间:用户933µs,系统:0 ns,总计:933µ壁时间:800µs
解决方案3:
CPU时间:用户0 ns,sys:1.35 ms,总计:1.35 ms壁时间:1.08 ms
解决方案4:
CPU时间:用户1.23毫秒,系统:45µs,总计:1.27毫秒壁时间:986µs
解决方案5:
CPU时间:用户1.09 ms,系统:19µs,总计:1.11 ms壁时间:949µs
解决方案6:
CPU时间:用户955µs,系统:34µs,总计:989µs壁时间:859µs
其他回答
在您的情况下,
df = df.reindex(columns=['mean',0,1,2,3,4])
会做你想做的事。
在我的情况下(一般形式):
df = df.reindex(columns=sorted(df.columns))
df = df.reindex(columns=(['opened'] + list([a for a in df.columns if a != 'opened']) ))
大多数答案都不够概括,panda reindex_axis方法有点乏味,因此我提供了一个简单的函数,可以使用字典将任意数量的列移动到任意位置,其中key=列名,value=要移动到的位置。如果数据帧很大,请将True传递给“big_data”,那么函数将返回有序的列列表。您可以使用此列表来分割数据。
def order_column(df, columns, big_data = False):
"""Re-Orders dataFrame column(s)
Parameters :
df -- dataframe
columns -- a dictionary:
key = current column position/index or column name
value = position to move it to
big_data -- boolean
True = returns only the ordered columns as a list
the user user can then slice the data using this
ordered column
False = default - return a copy of the dataframe
"""
ordered_col = df.columns.tolist()
for key, value in columns.items():
ordered_col.remove(key)
ordered_col.insert(value, key)
if big_data:
return ordered_col
return df[ordered_col]
# e.g.
df = pd.DataFrame({'chicken wings': np.random.rand(10, 1).flatten(), 'taco': np.random.rand(10,1).flatten(),
'coffee': np.random.rand(10, 1).flatten()})
df['mean'] = df.mean(1)
df = order_column(df, {'mean': 0, 'coffee':1 })
>>>
col = order_column(df, {'mean': 0, 'coffee':1 }, True)
col
>>>
['mean', 'coffee', 'chicken wings', 'taco']
# you could grab it by doing this
df = df[col]
我想到了和Dmitriy Work一样的答案,显然是最简单的:
df["mean"] = df.mean(1)
l = list(np.arange(0,len(df.columns) -1 ))
l.insert(0,-1)
df.iloc[:,l]
要根据其他列的名称将现有列设置为右侧/左侧,请执行以下操作:
def df_move_column(df, col_to_move, col_left_of_destiny="", right_of_col_bool=True):
cols = list(df.columns.values)
index_max = len(cols) - 1
if not right_of_col_bool:
# set left of a column "c", is like putting right of column previous to "c"
# ... except if left of 1st column, then recursive call to set rest right to it
aux = cols.index(col_left_of_destiny)
if not aux:
for g in [x for x in cols[::-1] if x != col_to_move]:
df = df_move_column(
df,
col_to_move=g,
col_left_of_destiny=col_to_move
)
return df
col_left_of_destiny = cols[aux - 1]
index_old = cols.index(col_to_move)
index_new = 0
if len(col_left_of_destiny):
index_new = cols.index(col_left_of_destiny) + 1
if index_old == index_new:
return df
if index_new < index_old:
index_new = np.min([index_new, index_max])
cols = (
cols[:index_new]
+ [cols[index_old]]
+ cols[index_new:index_old]
+ cols[index_old + 1 :]
)
else:
cols = (
cols[:index_old]
+ cols[index_old + 1 : index_new]
+ [cols[index_old]]
+ cols[index_new:]
)
df = df[cols]
return df
E.g.
cols = list("ABCD")
df2 = pd.DataFrame(np.arange(4)[np.newaxis, :], columns=cols)
for k in cols:
print(30 * "-")
for g in [x for x in cols if x != k]:
df_new = df_move_column(df2, k, g)
print(f"{k} after {g}: {df_new.columns.values}")
for k in cols:
print(30 * "-")
for g in [x for x in cols if x != k]:
df_new = df_move_column(df2, k, g, right_of_col_bool=False)
print(f"{k} before {g}: {df_new.columns.values}")
输出:
你也可以这样做:
df = df[['mean', '0', '1', '2', '3']]
您可以通过以下方式获取列列表:
cols = list(df.columns.values)
输出将产生:
['0', '1', '2', '3', 'mean']
…然后,在将其放入第一个函数之前,可以手动重新排列