You can multiply two columns in a pandas DataFrame and add the result into a new column using simple arithmetic operations. Here's how to do it:
Assuming you have a DataFrame named df with columns column1 and column2, and you want to create a new column result that stores the product of these two columns:
import pandas as pd # Create a sample DataFrame data = {'column1': [1, 2, 3, 4], 'column2': [5, 6, 7, 8]} df = pd.DataFrame(data) # Multiply the two columns and add the result to a new column df['result'] = df['column1'] * df['column2'] # Display the updated DataFrame print(df) In this example:
We create a sample DataFrame named df with two columns, column1 and column2.
We use the df['column1'] * df['column2'] expression to multiply the values in column1 and column2 element-wise, which results in a new pandas Series.
We assign this new Series to a new column named result in the DataFrame using df['result'] =.
After running this code, the df DataFrame will have a new column result that contains the products of column1 and column2.
Pandas multiply two columns and add result into a new column using arithmetic operation
import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column df['C'] = df['A'] * df['B'] print(df) Pandas multiply two columns and add result into a new column using lambda function
apply() to multiply two columns in a Pandas DataFrame and add the result into a new column.import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column using lambda function df['C'] = df.apply(lambda row: row['A'] * row['B'], axis=1) print(df) Pandas multiply two columns and add result into a new column using assign()
assign() method to multiply two columns in a Pandas DataFrame and add the result into a new column.import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column using assign() df = df.assign(C=df['A'] * df['B']) print(df) Pandas multiply two columns and add result into a new column using loc[]
loc[] indexer to perform element-wise multiplication between two columns in a Pandas DataFrame and add the result into a new column.import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column using loc[] df.loc[:, 'C'] = df['A'] * df['B'] print(df) Pandas multiply two columns and add result into a new column using applymap()
applymap() method to apply an element-wise function to each element of two columns in a Pandas DataFrame and add the result into a new column.import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column using applymap() df['C'] = df[['A', 'B']].applymap(lambda x: x[0] * x[1]).sum(axis=1) print(df) Pandas multiply two columns and add result into a new column using numpy
import pandas as pd import numpy as np # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column using numpy df['C'] = np.multiply(df['A'], df['B']) print(df) Pandas multiply two columns and add result into a new column using dot product
import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column using dot product df['C'] = df['A'].dot(df['B']) print(df) Pandas multiply two columns and add result into a new column using eval()
eval() method to perform element-wise multiplication between two columns in a Pandas DataFrame and add the result into a new column.import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column using eval() df['C'] = pd.eval('df.A * df.B') print(df) Pandas multiply two columns and add result into a new column using iterrows()
iterrows() method to iterate over rows in a Pandas DataFrame, multiply two columns element-wise, and add the result into a new column.import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column using iterrows() df['C'] = [row['A'] * row['B'] for index, row in df.iterrows()] print(df) Pandas multiply two columns and add result into a new column using map()
map() method to apply a function that multiplies two columns element-wise and add the result into a new column.import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Multiply two columns and add result into a new column using map() df['C'] = df['A'].map(lambda x: x * df['B'][df.index[df['A'] == x][0]]) print(df) scala-gatling xlrd xcode6 share numpy-ndarray prometheus-alertmanager xml-deserialization modality mplot3d zipcode