pandas DataFrame Manipulation in Python – Operations & Editing (7 Examples)
This article illustrates how to edit pandas DataFrames in the Python programming language.
Setting up the Examples
import pandas as pd # Import pandas library |
import pandas as pd # Import pandas library
my_df = pd.DataFrame({"A":["y", "x", "c", "v", "b"], # Construct new pandas DataFrame "B":range(25, 30), "C":range(20, 15, - 1)}) print(my_df) # Display pandas DataFrame # A B C # 0 y 25 20 # 1 x 26 19 # 2 c 27 18 # 3 v 28 17 # 4 b 29 16 |
my_df = pd.DataFrame({"A":["y", "x", "c", "v", "b"], # Construct new pandas DataFrame "B":range(25, 30), "C":range(20, 15, - 1)}) print(my_df) # Display pandas DataFrame # A B C # 0 y 25 20 # 1 x 26 19 # 2 c 27 18 # 3 v 28 17 # 4 b 29 16
Example 1: Adding New Variable to pandas DataFrame
D = [1, 2, 3, 4, 5] # Creating list object print(D) # [1, 2, 3, 4, 5] |
D = [1, 2, 3, 4, 5] # Creating list object print(D) # [1, 2, 3, 4, 5]
df1 = my_df.assign(D = D) # Adding list as new column print(df1) # A B C D # 0 y 25 20 1 # 1 x 26 19 2 # 2 c 27 18 3 # 3 v 28 17 4 # 4 b 29 16 5 |
df1 = my_df.assign(D = D) # Adding list as new column print(df1) # A B C D # 0 y 25 20 1 # 1 x 26 19 2 # 2 c 27 18 3 # 3 v 28 17 4 # 4 b 29 16 5
Example 2: Merging Two pandas DataFrames
df2 = pd.DataFrame({"A":["c", "v", "b", "n"], # Construct another pandas DataFrame "D":range(50, 54), "E":range(60, 64)}) print(df2) # A D E # 0 c 50 60 # 1 v 51 61 # 2 b 52 62 # 3 n 53 63 |
df2 = pd.DataFrame({"A":["c", "v", "b", "n"], # Construct another pandas DataFrame "D":range(50, 54), "E":range(60, 64)}) print(df2) # A D E # 0 c 50 60 # 1 v 51 61 # 2 b 52 62 # 3 n 53 63
df3 = pd.merge(my_df, df2, on = "A", how = "left") # Left join print(df3) # A B C D E # 0 y 25 20 NaN NaN # 1 x 26 19 NaN NaN # 2 c 27 18 50.0 60.0 # 3 v 28 17 51.0 61.0 # 4 b 29 16 52.0 62.0 |
df3 = pd.merge(my_df, df2, on = "A", how = "left") # Left join print(df3) # A B C D E # 0 y 25 20 NaN NaN # 1 x 26 19 NaN NaN # 2 c 27 18 50.0 60.0 # 3 v 28 17 51.0 61.0 # 4 b 29 16 52.0 62.0
Example 3: Dropping Column from pandas DataFrame
df4 = my_df.drop("A", axis = 1) # Remove column from DataFrame print(df4) # B C # 0 25 20 # 1 26 19 # 2 27 18 # 3 28 17 # 4 29 16 |
df4 = my_df.drop("A", axis = 1) # Remove column from DataFrame print(df4) # B C # 0 25 20 # 1 26 19 # 2 27 18 # 3 28 17 # 4 29 16
Example 4: Appending New Row to pandas DataFrame
add_row = [1, 2, 3] # Creating list as new row print(add_row) |
add_row = [1, 2, 3] # Creating list as new row print(add_row)
df5 = my_df.copy() # Adding new row to DataFrame df5.loc[6] = add_row print(df5) # A B C # 0 y 25 20 # 1 x 26 19 # 2 c 27 18 # 3 v 28 17 # 4 b 29 16 # 6 1 2 3 |
df5 = my_df.copy() # Adding new row to DataFrame df5.loc[6] = add_row print(df5) # A B C # 0 y 25 20 # 1 x 26 19 # 2 c 27 18 # 3 v 28 17 # 4 b 29 16 # 6 1 2 3
Example 5: Concatenating Rows of Two pandas DataFrames
df6 = pd.DataFrame({"A":range(0, 5), # Construct another pandas DataFrame "B":range(10, 15), "C":range(20, 25)}) print(df6) # A B C # 0 0 10 20 # 1 1 11 21 # 2 2 12 22 # 3 3 13 23 # 4 4 14 24 |
df6 = pd.DataFrame({"A":range(0, 5), # Construct another pandas DataFrame "B":range(10, 15), "C":range(20, 25)}) print(df6) # A B C # 0 0 10 20 # 1 1 11 21 # 2 2 12 22 # 3 3 13 23 # 4 4 14 24
df7 = pd.concat([my_df, df6]) # Appending two DataFrames print(df7) # A B C # 0 y 25 20 # 1 x 26 19 # 2 c 27 18 # 3 v 28 17 # 4 b 29 16 # 0 0 10 20 # 1 1 11 21 # 2 2 12 22 # 3 3 13 23 # 4 4 14 24 |
df7 = pd.concat([my_df, df6]) # Appending two DataFrames print(df7) # A B C # 0 y 25 20 # 1 x 26 19 # 2 c 27 18 # 3 v 28 17 # 4 b 29 16 # 0 0 10 20 # 1 1 11 21 # 2 2 12 22 # 3 3 13 23 # 4 4 14 24
Example 6: Dropping Row from pandas DataFrame
df8 = my_df[my_df.B >= 27] # Removing particular rows print(df8) # A B C # 2 c 27 18 # 3 v 28 17 # 4 b 29 16 |
df8 = my_df[my_df.B >= 27] # Removing particular rows print(df8) # A B C # 2 c 27 18 # 3 v 28 17 # 4 b 29 16
Example 7: Exchanging Values in pandas DataFrame
df8 = my_df.copy() # Replace certain values in column df8["B"] = df8["B"].replace(28, 1000) print(df8) # A B C # 0 y 25 20 # 1 x 26 19 # 2 c 27 18 # 3 v 1000 17 # 4 b 29 16 |
df8 = my_df.copy() # Replace certain values in column df8["B"] = df8["B"].replace(28, 1000) print(df8) # A B C # 0 y 25 20 # 1 x 26 19 # 2 c 27 18 # 3 v 1000 17 # 4 b 29 16
Further Resources
Furthermore, you may read the other posts on my website. Some tutorials are listed below.