Pandas: Get the numeric representation of an array by identifying distinct values of a given column of a DataFrame
77. Get Numeric Representation from Distinct Values
Write a Pandas program to get the numeric representation of an array by identifying distinct values of a given column of a DataFrame.
Sample Solution :
Python Code :
import pandas as pd
df = pd.DataFrame({
'Name': ['Alberto Franco','Gino Mcneill','Ryan Parkes', 'Eesha Hinton', 'Gino Mcneill'],
'Date_Of_Birth ': ['17/05/2002','16/02/1999','25/09/1998','11/05/2002','15/09/1997'],
'Age': [18.5, 21.2, 22.5, 22, 23]
})
print("Original DataFrame:")
print(df)
label1, unique1 = pd.factorize(df['Name'])
print("\nNumeric representation of an array by identifying distinct values:")
print(label1)
print(unique1)
Sample Output:
Original DataFrame: Name Date_Of_Birth Age 0 Alberto Franco 17/05/2002 18.5 1 Gino Mcneill 16/02/1999 21.2 2 Ryan Parkes 25/09/1998 22.5 3 Eesha Hinton 11/05/2002 22.0 4 Gino Mcneill 15/09/1997 23.0 Numeric representation of an array by identifying distinct values: [0 1 2 3 1] Index(['Alberto Franco', 'Gino Mcneill', 'Ryan Parkes', 'Eesha Hinton'], dtype='object')
For more Practice: Solve these Related Problems:
- Write a Pandas program to encode the distinct values of a categorical column into unique integers and then output the mapping.
- Write a Pandas program to convert a column of non-numeric values into numeric codes using factorize() and then display the results.
- Write a Pandas program to map unique strings in a column to numbers and then create a new column with these numeric representations.
- Write a Pandas program to identify distinct values in a column and then use LabelEncoder to transform them into numeric form.
Go to:
Previous: Write a Pandas program to clean object column with mixed data of a given DataFrame using regular expression.
Next: Write a Pandas program to replace the current value in a dataframe column based on last largest value. If the current value is less than last largest value replaces the value with 0.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.