在不丢失或弄乱数据的情况下更改数据帧中的行顺序

2024-05-02 20:12:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧:

(Index)    sample    reads yeasts    
9          CO ref    10
10         CO raai   20
11         CO tus    30

我想根据sample更改列的顺序,预期输出:

(Index)    sample    reads yeasts    
9          CO ref    10
11         CO tus    30
10         CO raai   10

我对行的索引不感兴趣

我根据其他stackoverflow/google帖子尝试了以下代码:

df=df.reindex(["CO ref","CO tus","CO raai"])

这会正确地更改索引,但所有其他列都会获得值nan

我也试过:

df.index=["CO ref","CO tus","CO raai"]  

这会正确地更改索引,但其他列不会切换,因此会弄乱数据帧

此外:

df["sample"].index=["CO ref","CO tus","CO raai"]   

但这没有任何作用

我怎样才能让它工作


Tags: 数据samplerefdfindex顺序googlestackoverflow
2条回答

对于reindex,需要从sample列创建索引:

df=df.set_index(['sample']).reindex(["CO ref","CO tus","CO raai"]).reset_index()

或使用有序分类:

cats = ["CO ref","CO tus","CO raai"]
df['sample'] = pd.CategoricalIndex(df['sample'], ordered=True, categories=cats)
df = df.sort_values('sample')

耶斯雷尔的解决方案当然是正确的,而且很可能是最快的。但由于这实际上只是一个重构数据帧的问题,我想向您展示如何轻松地做到这一点,同时让您的过程选择要使用的排序列的子集

以下非常简单的函数将允许您指定数据帧的子集和顺序:

# function to subset and order a pandas
# dataframe of a long format
def order_df(df_input, order_by, order):
    df_output=pd.DataFrame()
    for var in order:    
        df_append=df_input[df_input[order_by]==var].copy()
        df_output = pd.concat([df_output, df_append])
    return(df_output)

下面是一个使用plotly express中的iris数据集的示例df['species'].unique()将显示该列的顺序:

输出:

array(['setosa', 'versicolor', 'virginica'], dtype=object)

现在,使用上面的函数运行下面的完整代码段将为您提供一个新的指定顺序。不需要分类变量或篡改索引

使用数据示例完成代码:

# imports
import pandas as pd
import plotly.express as px

# data
df = px.data.iris()

# function to subset and order a pandas
# dataframe fo a long format
def order_df(df_input, order_by, order):
    df_output=pd.DataFrame()
    for var in order:    
        df_append=df_input[df_input[order_by]==var].copy()
        df_output = pd.concat([df_output, df_append])
    return(df_output)

# data subsets
df_new = order_df(df_input = df, order_by='species', order=['virginica', 'setosa', 'versicolor'])
df_new['species'].unique()

输出:

array(['virginica', 'setosa', 'versicolor'], dtype=object)

相关问题 更多 >