我想知道是否有一种简单的方法可以使用 pandas 来重新定位数据框中的单个列,该方法可以模拟 dplyr 函数(在 R 中)“重新定位”。由于我通过几次搜索没有找到任何与此非常接近的内容,因此我和一位工作中的朋友创建了一个简单的函数来使用 python 执行此操作,我认为与可能需要它的每个人分享它会很好。
def relocate_column(df, relocated_col, reference_col,distance=1):
# Relocates a column based on another column's position
# df = dataframe in question
# relocated_col = column that will be relocated
# reference_col = column that will be used as reference
# distance = how many columns from the reference will the relocated column be placed.
# Positive 'distance' is after, negative is before. +1 is immediately after, -1 is immediately before
# List all columns in df
df_columns = list(df.columns)
# Check if both columns exist
if reference_col not in df_columns:
print(f'Column {reference_col} not found in dataframe')
return df
if relocated_col not in df_columns:
print(f'Column {relocated_col} not found in dataframe')
return df
# Relocates the column based on inputed distance (default = 1)
df_columns.remove(relocated_col)
df_columns.insert(df_columns.index(reference_col) + distance, relocated_col)
# Applies new order to dataframe
df = df[df_columns]
return df
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [10, 2, 1, 8]},
index=['falcon', 'dog', 'spider', 'fish'])
print(df)
num_legs num_wings num_specimen_seen
falcon 2 2 10
dog 4 0 2
spider 8 0 1
fish 0 0 8
df = relocate_column(df, 'num_wings', 'num_legs')
print(df)
num_wings num_legs num_specimen_seen
falcon 2 2 10
dog 0 4 2
spider 0 8 1
fish 0 0 8