Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert Series to Dataframe in Python

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading