“Fungsi Panda Lambda Mengembalikan DataFrame” Kode Jawaban

DataFrame fungsi lambda

# 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
Africodes

DataFrame fungsi lambda

# 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
Africodes

Fungsi Panda Lambda Mengembalikan DataFrame

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

Fungsi Panda Lambda Mengembalikan DataFrame

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

Jawaban yang mirip dengan “Fungsi Panda Lambda Mengembalikan DataFrame”

Pertanyaan yang mirip dengan “Fungsi Panda Lambda Mengembalikan DataFrame”

Lebih banyak jawaban terkait untuk “Fungsi Panda Lambda Mengembalikan DataFrame” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya