Home > database >  Convert Series to Dataframe in Python
Convert Series to Dataframe in Python

Time:11-11

We need to convert three different series to a dataframe in Python 3.

Series 1 (Mean):

e1     40.355
e2     59.556
ec1    17.668
ec2    75.230

Series 2 (SD):

e1     19.815
e2     17.550
ec1    14.704
ec2    54.485

Series 3 (Max):

e1     87.36
e2     80.39
ec1    200.3
ec2    207.5

Above needs to be merged into a dataframe that looks like the following:

Features     Mean       Standard Deviation     Max

e1           40.355     19.815                 87.36
e2           59.556     17.550                 80.39
ec1          17.668     14.704                 200.3
ec2          75.230     54.485                 207.5

What we have done so far is:

if __name__ == '__main__':

    Mean = df.mean().round(3)
    SD = df.std().round(3)
    Max = df.max()

    df2 = pd.DataFrame(columns=['Features', 'Mean', 'Standard Deviation', 'Max'])

    df2['Mean'] = Mean
    df2['Standard Deviation'] = SD
    df2['Max'] = Max

    print(df2)

Above prints/outputs the following:

        Features    Mean  Standard Deviation      Max
e1           NaN  40.355              19.815    87.36
e2           NaN  59.556              17.550    80.39
ec1          NaN  17.668              14.704    200.3
ec2          NaN  75.230              54.485    207.5

How can we set the index to features column such that the dataframe looks like the following:

        Features    Mean  Standard Deviation      Max
            e1    40.355              19.815    87.36
            e2    59.556              17.550    80.39
           ec1    17.668              14.704    200.3
           ec2    75.230              54.485    207.5

CodePudding user response:

Use concat, rename_axis and reset_index:

df2 = (pd.concat([Mean, SD, Max], 
                 keys=["Mean", "SD", "Max"], 
                 axis=1)
       .rename_axis(index="Features")
       .reset_index())

>>> df2
  Features    Mean      SD     Max
0       e1  40.355  19.815   87.36
1       e2  59.556  17.550   80.39
2      ec1  17.668  14.704  200.30
3      ec2  75.230  54.485  207.50
  • Related