def order(frame,var):if type(var) is str:var = [var] #let the command take a string or listvarlist =[w for w in frame.columns if w not in var]frame = frame[var+varlist]return frame
meanDf = pd.DataFrame(df.pop('mean'))# now df doesn't contain "mean" anymore. Order of join will move it to left or right:meanDf.join(df) # has mean as first columndf.join(meanDf) # has mean as last column
def mean_first(df):ncols = df.shape[1] # Get the number of columnsindex = list(range(ncols)) # Create an index to reorder the columnsindex.insert(0,ncols) # This puts the last column at the frontreturn(df.assign(mean=df.mean(1)).iloc[:,index]) # new df with last column (mean) first
def order_column(df, columns, big_data = False):
"""Re-Orders dataFrame column(s)Parameters :df -- dataframecolumns -- a dictionary:key = current column position/index or column namevalue = position to move it tobig_data -- booleanTrue = returns only the ordered columns as a listthe user user can then slice the data using thisordered columnFalse = 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]
import numpy as npimport pandas as pddf = 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)
def rearrange_list(input_list, input_item_to_move, input_item_insert_here):'''Helper function to re-arrange the order of items in a list.Useful for moving column in pandas dataframe.
Inputs:input_list - listinput_item_to_move - item in list to moveinput_item_insert_here - item in list, insert before
returns:output_list'''# make copy for output, make sure it's a listoutput_list = list(input_list)
# index of item to moveidx_move = output_list.index(input_item_to_move)
# pop off the item to moveitm_move = output_list.pop(idx_move)
# index of item to insert hereidx_insert = output_list.index(input_item_insert_here)
# insert item to move into hereoutput_list.insert(idx_insert, itm_move)
return output_list
import pandas as pd
# step 1: create sample dataframedf = pd.DataFrame({'motorcycle': ['motorcycle1', 'motorcycle2', 'motorcycle3'],'initial_odometer': [101, 500, 322],'final_odometer': [201, 515, 463],'other_col_1': ['blah', 'blah', 'blah'],'other_col_2': ['blah', 'blah', 'blah']})print('Step 1: create sample dataframe')display(df)print()
# step 2: add new column that is difference between final and initialdf['change_odometer'] = df['final_odometer']-df['initial_odometer']print('Step 2: add new column')display(df)print()
# step 3: rearrange columnsls_cols = df.columnsls_cols = rearrange_list(ls_cols, 'change_odometer', 'final_odometer')df=df[ls_cols]print('Step 3: rearrange columns')display(df)
def reorder_df_columns(df, start=None, end=None):"""This function reorder columns of a DataFrame.It takes columns given in the list `start` and move them to the left.Its also takes columns in `end` and move them to the right."""if start is None:start = []if end is None:end = []assert isinstance(start, list) and isinstance(end, list)cols = list(df.columns)for c in start:if c not in cols:start.remove(c)for c in end:if c not in cols or c in start:end.remove(c)for c in start + end:cols.remove(c)cols = start + cols + endreturn df[cols]
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 itaux = 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 dfcol_left_of_destiny = cols[aux - 1]
index_old = cols.index(col_to_move)index_new = 0if 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
例如。
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}")
def order(dataframe,cols,f_or_l=None,before=None, after=None):
#만든이: 김완석, Stata로 뚝딱뚝딱 저자, blog.naver.com/sanzo213 운영# 갖다 쓰시거나 수정을 하셔도 되지만 출처는 꼭 밝혀주세요# cols옵션 및 befor/after옵션에 튜플이 가능하게끔 수정했으며, 오류문구 수정함(2021.07.12,1)# 칼럼이 멀티인덱스인 상태에서 reset_index()메소드 사용했을 시 적용안되는 걸 수정함(2021.07.12,2)
import pandas as pdif (type(cols)==str) or (type(cols)==int) or (type(cols)==float) or (type(cols)==bool) or type(cols)==tuple:cols=[cols]
dd=list(dataframe.columns)for i in cols:idd.remove(i) #cols요소를 제거함
if (f_or_l==None) & ((before==None) & (after==None)):print('f_or_l옵션을 쓰시거나 아니면 before옵션/after옵션 쓰셔야되요')
if ((f_or_l=='first') or (f_or_l=='last')) & ~((before==None) & (after==None)):print('f_or_l옵션 사용시 before after 옵션 사용불가입니다.')
if (f_or_l=='first') & (before==None) & (after==None):new_order=cols+dddataframe=dataframe[new_order]return dataframe
if (f_or_l=='last') & (before==None) & (after==None):new_order=dd+colsdataframe=dataframe[new_order]return dataframe
if (before!=None) & (after!=None):print('before옵션 after옵션 둘다 쓸 수 없습니다.')
if (before!=None) & (after==None) & (f_or_l==None):
if not((type(before)==str) or (type(before)==int) or (type(before)==float) or(type(before)==bool) or ((type(before)!=list)) or((type(before)==tuple))):print('before옵션은 칼럼 하나만 입력가능하며 리스트 형태로도 입력하지 마세요.')
else:b=dd[:dd.index(before)]a=dd[dd.index(before):]
new_order=b+cols+adataframe=dataframe[new_order]return dataframe
if (after!=None) & (before==None) & (f_or_l==None):
if not((type(after)==str) or (type(after)==int) or (type(after)==float) or(type(after)==bool) or ((type(after)!=list)) or((type(after)==tuple))):
print('after옵션은 칼럼 하나만 입력가능하며 리스트 형태로도 입력하지 마세요.')
else:b=dd[:dd.index(after)+1]a=dd[dd.index(after)+1:]
new_order=b+cols+adataframe=dataframe[new_order]return dataframe
# module
import pandas as pdimport numpy as npfrom order import order # call order function from order.py file
# make a dataset
columns='a b c d e f g h i j k'.split()dic={}
n=-1for i in columns:
n+=1dic[i]=list(range(1+n,10+1+n))data=pd.DataFrame(dic)print(data)
# use order function (1) : order column e in the first
data2=order(data,'e',f_or_l='first')print(data2)
# use order function (2): order column e in the last , "data" dataframe
print(order(data,'e',f_or_l='last'))
# use order function (3) : order column i before column c in "data" dataframe
print(order(data,'i',before='c'))
# use order function (4) : order column g after column b in "data" dataframe
print(order(data,'g',after='b'))
# use order function (4) : order columns ['c', 'd', 'e'] after column i in "data" dataframe
print(order(data,['c', 'd', 'e'],after='i'))