Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

lambda function dataframe

# importing pandas library
import pandas as pd
   
# creating and initializing a list
values= [['Rohan',455],['Elvish',250],['Deepak',495],
         ['Soni',400],['Radhika',350],['Vansh',450]] 
  
# creating a pandas dataframe
df = pd.DataFrame(values,columns=['Name','Total_Marks'])
  
# Applying lambda function to find 
# percentage of 'Total_Marks' column 
# using df.assign()
df = df.assign(Percentage = lambda x: (x['Total_Marks'] /500 * 100))
  
# displaying the data frame
df
Comment

lambda function dataframe

# importing pandas library
import pandas as pd
  
# creating and initializing a nested list
values_list = [[15, 2.5, 100], [20, 4.5, 50], [25, 5.2, 80],
               [45, 5.8, 48], [40, 6.3, 70], [41, 6.4, 90],
               [51, 2.3, 111]]
  
# creating a pandas dataframe
df = pd.DataFrame(values_list, columns=['Field_1', 'Field_2', 'Field_3'])
  
# Applying lambda function to find 
# the product of 3 columns using 
# df.assign()
df = df.assign(Product=lambda x: (x['Field_1'] * x['Field_2'] * x['Field_3']))
  
# printing dataframe
df
Comment

panda lambda function returns dataframe

df = df.assign(total_cost=lambda x: TotalCost(x['Distance'], m, c))
Comment

panda lambda function returns dataframe

df.eval('total_cost = @m * Distance + @c', inplace=True)
Comment

PREVIOUS NEXT
Code Example
Python :: How to Loop Through Tuples using for loop in python 
Python :: np.append 
Python :: python upload file to s3 
Python :: convert method to str python 
Python :: pyqt5 hide button 
Python :: matplotlib use marker along variable 
Python :: numpy argsort 
Python :: python empty list 
Python :: Python NumPy insert Function Example Working with arrays 
Python :: permutation and combination program in python 
Python :: max value of a list prolog 
Python :: python named tuples 
Python :: atoi in python code 
Python :: get_permissions 
Python :: polymorphism in python 
Python :: removing stop words from the text 
Python :: Python DateTime Date Class Syntax 
Python :: python basic data types 
Python :: precision accuracy recall python example 
Python :: get array from h5py dataset 
Python :: python - input: integer 
Python :: what is serializer in django 
Python :: object oriented programming python 
Python :: Program to Compute LCM Using GCD 
Python :: list add pythhon 
Python :: add all elements of list to set python 
Python :: sparse matrix multiplication in python 
Python :: How to JOIN three tables with Django ORM 
Python :: função map python 
Python :: swap list 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =