Home »
Python »
Python Programs
What does the term broadcasting mean in Pandas documentation?
Learn about the team 'broadcasting' mean in pandas documentation.
By Pranit Sharma Last updated : October 02, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
The term "broadcasting" originates with the concept of NumPy, it simply explains the format of the output (the final result in the console window) that will result when we perform some operations on NumPy arrays.
We are going to perform some operations on the different types of arrays.
1) Creating a series and multiplying it by a scalar value
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating a series
ser = pd.Series([10,30,50,70,90])
# Display Original Series
print("Created Series:\n",ser,"\n")
# Multiplying by a scalar value
res = ser*5
# Display result
print("Result:\n",res)
Output
The output of the above program is:
2) Performing same operation on DataFrame
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating a DataFrame
df = pd.DataFrame({'a':np.random.randn(4), 'b':np.random.randn(4)})
# Display Original DataFrame
print("Created DataFrame:\n",df,"\n")
# Multiplying by a scalar value
res = df*5
# Display result
print("Result:\n",res)
Output
The output of the above program is:
3) Performing same operation but this time we will multiply all the columns with one column only and not a scalar value.
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating a DataFrame
df = pd.DataFrame({'a':np.random.randn(4), 'b':np.random.randn(4)})
# Display Original DataFrame
print("Created DataFrame:\n",df,"\n")
# Multiplying by a scalar value
res = df + df.iloc[0]
# Display result
print("Result:\n",res)
Output
The output of the above program is:
Python Pandas Programs »