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 :: group by 2 columns pandas 
Python :: Python IDLE Shell Run Command 
Python :: np.tanh 
Python :: Python round to only two decimal 
Python :: code folding vim python 
Python :: tkinter frameless window 
Python :: win64pyinstaller 
Python :: pip install opencv 
Python :: Python Frozenset() for Dictionary 
Python :: how to search for a data in excel pandas 
Python :: how to print data type in python 
Python :: data where values in column starts with particular value 
Python :: python elapsed time in milliseconds 
Python :: rust vs python 
Python :: shallow copy in python 
Python :: how to get all index of a char of a string in python 
Python :: gpt-3 tokenizer python3 
Python :: if string in list py 
Python :: how to make a random int in python 
Python :: create a window using tkinter 
Python :: map function in python 
Python :: delete and start fresh with db django 
Python :: gensim show_topics get topic 
Python :: what is django 
Python :: pycord discord discordpy get total slash commands and total commands regestered in your bot 
Python :: convert .py to exe 
Python :: python json write utf 8 
Python :: subset in python 
Python :: dataframe of one row 
Python :: create random phone number python 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =