Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas lamda column reference

# importing pandas and numpylibraries
import pandas as pd
import numpy as np
 
# creating and initializing a nested list
values_list = [[1.5, 2.5, 10.0], [2.0, 4.5, 5.0], [2.5, 5.2, 8.0],
               [4.5, 5.8, 4.8], [4.0, 6.3, 70], [4.1, 6.4, 9.0],
               [5.1, 2.3, 11.1]]
 
# creating a pandas dataframe
df = pd.DataFrame(values_list, columns=['Field_1', 'Field_2', 'Field_3'],
                  index=['a', 'b', 'c', 'd', 'e', 'f', 'g'])
 
 
# Apply function numpy.square() to square
# the values of 2 rows only i.e. with row
# index name 'b' and 'f' only
df = df.apply(lambda x: np.square(x) if x.name in ['b', 'f'] else x, axis=1)
 
# Applying lambda function to find product of 3 columns
# i.e 'Field_1', 'Field_2' and 'Field_3'
df = df.assign(Product=lambda x: (x['Field_1'] * x['Field_2'] * x['Field_3']))
 
 
# printing dataframe
df
Comment

PREVIOUS NEXT
Code Example
Python :: python how to print input 
Python :: networkx max degree node 
Python :: loop over twodimensional array python 
Python :: python shortest distance between two points 
Python :: reportlab python draw line 
Python :: python scheduling 
Python :: django id 
Python :: python dict to dataclass 
Python :: np.random.normal 
Python :: k choose n python 
Python :: crear una clase en python 
Python :: sort eigenvalues and eigenvectors python 
Python :: click ok on alert box selenium webdriver python 
Python :: concatenate int to string python 
Python :: python sort class by attribute 
Python :: create array with unknown size in python 
Python :: count repeated characters in a string python 
Python :: Python program to print all odd numbers in a range 
Python :: python remove space from end of string 
Python :: flask client ip 
Python :: python drop the first word 
Python :: count down for loop python 
Python :: how to add two numbers 
Python :: delete key value in dictionary python 
Python :: print( n ) in python 
Python :: whatsapp online tracker python script 
Python :: cd in python 
Python :: how yo import python lib 
Python :: random number generator in python 
Python :: pandas read excel with two headers 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =