What is pandas & Why You Should Use It in Python (3 Examples)
pandas is an open-source software library for the Python programming language that is used to manipulate DataFrames. In this Python tutorial you’ll learn how to apply the functions of the pandas library.
Example Data
import pandas as pd # Load pandas |
import pandas as pd # Load pandas
my_df = pd.DataFrame({"A":range(5, 12), # Construct pandas DataFrame in Python "B":["a", "x", "b", "y", "c", "d", "x"], "C":range(1, 8)}) print(my_df) # A B C # 0 5 a 1 # 1 6 x 2 # 2 7 b 3 # 3 8 y 4 # 4 9 c 5 # 5 10 d 6 # 6 11 x 7 |
my_df = pd.DataFrame({"A":range(5, 12), # Construct pandas DataFrame in Python "B":["a", "x", "b", "y", "c", "d", "x"], "C":range(1, 8)}) print(my_df) # A B C # 0 5 a 1 # 1 6 x 2 # 2 7 b 3 # 3 8 y 4 # 4 9 c 5 # 5 10 d 6 # 6 11 x 7
Example 1: Dropping Rows of pandas DataFrame in Python
my_df1 = my_df[my_df.A > 7] # Dropping rows of DataFrame print(my_df1) # A B C # 3 8 y 4 # 4 9 c 5 # 5 10 d 6 # 6 11 x 7 |
my_df1 = my_df[my_df.A > 7] # Dropping rows of DataFrame print(my_df1) # A B C # 3 8 y 4 # 4 9 c 5 # 5 10 d 6 # 6 11 x 7
Example 2: Appending New Variable to pandas DataFrame in Python
D = ["d", "h", "h", "h", "d", "d", "d"] # Constructing new column print(D) # ['d', 'h', 'h', 'h', 'd', 'd', 'd'] |
D = ["d", "h", "h", "h", "d", "d", "d"] # Constructing new column print(D) # ['d', 'h', 'h', 'h', 'd', 'd', 'd']
my_df2 = my_df.assign(D = D) # Adding new column to DataFrame print(my_df2) # A B C D # 0 5 a 1 d # 1 6 x 2 h # 2 7 b 3 h # 3 8 y 4 h # 4 9 c 5 d # 5 10 d 6 d # 6 11 x 7 d |
my_df2 = my_df.assign(D = D) # Adding new column to DataFrame print(my_df2) # A B C D # 0 5 a 1 d # 1 6 x 2 h # 2 7 b 3 h # 3 8 y 4 h # 4 9 c 5 d # 5 10 d 6 d # 6 11 x 7 d
Example 3: Computing Variance of pandas DataFrame Variable in Python
my_df_var = my_df["A"].var() # Calculate variance of column print(my_df_var) # 4.666666666666667 |
my_df_var = my_df["A"].var() # Calculate variance of column print(my_df_var) # 4.666666666666667