Python – Reshape the data in a Pandas DataFrame

PythonServer Side ProgrammingProgramming

We can easily reshape the data by categorizing a specific column. Here, we will categorize the “Result”column i.e. Pass and Fail values in numbers form.

Import the required library −

import pandas as pd

Create a DataFrame with 2 columns −

dataFrame = pd.DataFrame(
   {
      "Student": ['Jack', 'Robin', 'Ted', 'Scarlett', 'Kat'],"Result": ['Pass', 'Fail', 'Fail', 'Pass', 'Pass']

   }
)

Reshape the data using the map() function and just set ‘Pass’ to 1 and ‘Fail’ to 0 −

dataFrame['Result'] = dataFrame['Result'].map({'Pass': 1,'Fail': 0, })

Example

Following is the code −

import pandas as pd

# Create DataFrame
dataFrame = pd.DataFrame(
   {
      "Student": ['Jack', 'Robin', 'Ted', 'Scarlett', 'Kat'],"Result": ['Pass', 'Fail', 'Fail', 'Pass', 'Pass']
   }
)

print"DataFrame ...\n",dataFrame

# reshaping into numbers
dataFrame['Result'] = dataFrame['Result'].map({'Pass': 1,'Fail': 0, })

print"\nReshaped DataFrame ...\n",dataFrame

Output

This will produce the following output

DataFrame ...
   Result   Student
0    Pass      Jack
1    Fail     Robin
2    Fail       Ted
3    Pass  Scarlett
4    Pass       Kat

Reshaped DataFrame ...
   Result   Student
0       1      Jack
1       0     Robin
2       0       Ted
3       1  Scarlett
4       1       Kat
raja
Published on 20-Sep-2021 07:34:11
Advertisements