How to use apply() and unique() for sample data set using python?

Description

To use apply(),unique() function for a
given data sample in python.

  • Import pandas library.
  • Load the sample data.
  • Create the date frame.
  • Use apply() and unique() functions

#Import library functions

import pandas as pd

import numpy as np

#sample data set

data={'salary'
:[100,200,300,400,500,400,
300,200,100,50], 'age':[25,
26,25,23,30,29,23,23,25,25],

'rating':[4,3.24,2.5,2.25,2,2.25,
2.5,2.75,3.2,4.2], 'bonus':[2500,1200,
900,3000,1800,1400,850,250,750, 1000]}

#create data frame

df=pd.DataFrame(data)

#call unique()

u=df['salary'].unique()

print("The result of unique() is\n",u)

#call apply()

a=df.apply(np.log)

print("The result of apply() is\n",a)

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit