在Python中应用函数后,如何更改数据帧的索引?

在Python中应用函数后,如何更改数据帧的索引?,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我有各种数据帧名称:step1、step2、step5、step7等等 我编写了一个函数,如下所示: def statistics(df): plus_one = df['BacksGas_Flow_sccm'][df['y_ocsvm'] == 1].describe() negative_one = df['BacksGas_Flow_sccm'][df['y_ocsvm'] == -1].describe() return plus_one, negative_on

我有各种
数据帧
名称:step1、step2、step5、step7等等

我编写了一个函数,如下所示:

def statistics(df):
    plus_one = df['BacksGas_Flow_sccm'][df['y_ocsvm'] == 1].describe()
    negative_one = df['BacksGas_Flow_sccm'][df['y_ocsvm'] == -1].describe()
    return plus_one, negative_one
其中
backgas\u Flow\u sccm
y\u ocsvm
是所有不同数据帧中的列名称

在此之后,我尝试创建一个新的数据帧,其中包含由
descripe()
返回的静态记录,我是通过以下方式完成的:

stats = pd.DataFrame(statistics(step1))
stats = stats.append(pd.DataFrame(statistics(step2)))
这给了我:

                    count          mean               std   min 25% 50% 75% max
BacksGas_Flow_sccm  1622.0  0.4370119194410199  0.11346778078574718 0.33333333333333304 0.33333333333333304 0.5 0.5 0.6666666666666665
BacksGas_Flow_sccm  426.0   0.19444444444444436 0.1873737774126198  0.0 0.16666666666666652 0.16666666666666652 0.16666666666666652 1.0
BacksGas_Flow_sccm  1285.0  0.5418071768266265  0.1998356616378414  0.2222222222222221  0.2222222222222221  0.6666666666666667  0.6666666666666667  0.6666666666666667
BacksGas_Flow_sccm  8028.0  0.4678901622100473  0.10157692912484724 0.0 0.4444444444444444  0.4444444444444444  0.5555555555555556  0.9999999999999998
我只想将索引名从
BacksGas\u Flow\u sccm
更改为它们所属的相应数据帧

预期输出:

         count         mean               std   min 25% 50% 75% max
Step1   1622.0  0.4370119194410199  0.11346778078574718 0.33333333333333304 0.33333333333333304 0.5 0.5 0.6666666666666665
Step1   426.0   0.19444444444444436 0.1873737774126198  0.0 0.16666666666666652 0.16666666666666652 0.16666666666666652 1.0
Step2   1285.0  0.5418071768266265  0.1998356616378414  0.2222222222222221  0.2222222222222221  0.6666666666666667  0.6666666666666667  0.6666666666666667
Step2   8028.0  0.4678901622100473  0.10157692912484724 0.0 0.4444444444444444  0.4444444444444444  0.5555555555555556  0.9999999999999998
我想知道这能做些什么


谢谢

您可以在
statistics
函数中执行此操作,并传入名称:

def statistics(df, name):
    plus_one = df['BacksGas_Flow_sccm'][df['y_ocsvm'] == 1].describe()
    negative_one = df['BacksGas_Flow_sccm'][df['y_ocsvm'] == -1].describe()
    ret_df = pd.DataFrame((plus_one, negative_one))
    ret_df['source'] = name

    return ret_df

stats = pd.DataFrame(statistics(step1, 'step1'))
stats = stats.append(pd.DataFrame(statistics(step2, 'step2')))

您可以在
statistics
函数中执行此操作,将名称传入:

def statistics(df, name):
    plus_one = df['BacksGas_Flow_sccm'][df['y_ocsvm'] == 1].describe()
    negative_one = df['BacksGas_Flow_sccm'][df['y_ocsvm'] == -1].describe()
    ret_df = pd.DataFrame((plus_one, negative_one))
    ret_df['source'] = name

    return ret_df

stats = pd.DataFrame(statistics(step1, 'step1'))
stats = stats.append(pd.DataFrame(statistics(step2, 'step2')))

这很难看,但这应该可以在不重复索引的情况下满足您的需求:

stats = pd.DataFrame(statistics(step1))
stats['step'] = 'Step1'
temp = pd.DataFrame(statistics(step2))
temp['step'] = 'Step2'
stats = stats.append(temp)
stats = stats.reset_index()

这很难看,但这应该可以在不重复索引的情况下满足您的需求:

stats = pd.DataFrame(statistics(step1))
stats['step'] = 'Step1'
temp = pd.DataFrame(statistics(step2))
temp['step'] = 'Step2'
stats = stats.append(temp)
stats = stats.reset_index()

不建议使用重复索引,为什么不将其作为列?这也是一种选择。关于如何实现这一点有什么建议吗?不建议使用重复索引,为什么不将其作为一列?这也是一种选择。对如何实现这一目标有何建议?