In this short guide, you’ll see how to get started with Pandas.
To begin, install Pandas using this command:
pip install pandas
Import pandas as pd
In order to use the Pandas package in Python, you’ll need to type “import pandas as pd” at the top of your code.
You’ll then be able to use “pd” to access various “pandas” functions and objects throughout your code.
Here is an example that demonstrates how to “import pandas as pd” and then use it to create a DataFrame with 2 columns:
import pandas as pd # Define the data data = { "Product": ["Laptop", "Tablet", "Smartphone", "Headphones", "Printer"], "Price": [1000, 300, 850, 150, 200] } # Create the DataFrame df = pd.DataFrame(data) # Print the DataFrame print(df)
The resulted DataFrame:
Product Price
0 Laptop 1000
1 Tablet 300
2 Smartphone 850
3 Headphones 150
4 Printer 200
Calculate the total sum
You can use “sum()” in order to calculate the total sum for the “Price” column:
import pandas as pd # Define the data data = { "Product": ["Laptop", "Tablet", "Smartphone", "Headphones", "Printer"], "Price": [1000, 300, 850, 150, 200] } # Create the DataFrame df = pd.DataFrame(data) # Calculate the total sum total_sum = df["Price"].sum() # Print the total sum print(total_sum)
The total sum is:
2500
Calculate the mean (average)
Use “mean()” to calculate the mean (average) for the “Price” column:
import pandas as pd # Define the data data = { "Product": ["Laptop", "Tablet", "Smartphone", "Headphones", "Printer"], "Price": [1000, 300, 850, 150, 200] } # Create the DataFrame df = pd.DataFrame(data) # Calculate the mean price mean_price = df["Price"].mean() # Print the mean price print(mean_price)
The resulted mean is:
500