How to append column in Pandas DataFrame

You can append a column in Pandas DataFrame with any of the following methods. I highly recommend you This book to learn Python. In this article, you will see 5 methods to append a column in Pandas DataFrame.

Method 1: df[‘Name’] = column

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict = {'Names':['Harry', 'John', 'Hussain', 'Messi'],
         'Marks':[95, 85, 77, 60]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(dict)

# Display the Original DataFrame
print(df)

# New Column
grade = ['A','B','C','D']

# Add a column in DataFrame
df['Grade'] = grade

# Display the DataFrame
print(df)

Output:

     Names  Marks
0    Harry     95
1     John     85
2  Hussain     77
3    Messi     60
     Names  Marks Grade
0    Harry     95     A
1     John     85     B
2  Hussain     77     C
3    Messi     60     D

Method 2: df.insert( )

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict = {'Names':['Harry', 'John', 'Hussain', 'Messi'],
         'Marks':[95, 85, 77, 60]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(dict)

# Display the Original DataFrame
print(df)

# Add a column in DataFrame (You can also change position of new column)
df.insert(2, "Grade", ['A','B','C','D'], True)

# Display the DataFrame
print(df)

Output:

     Names  Marks
0    Harry     95
1     John     85
2  Hussain     77
3    Messi     60
     Names  Marks Grade
0    Harry     95     A
1     John     85     B
2  Hussain     77     C
3    Messi     60     D

Method 3: df.assign( )

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict = {'Names':['Harry', 'John', 'Hussain', 'Messi'],
         'Marks':[95, 85, 77, 60]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(dict)

# Display the Original DataFrame
print(df)

# Add a column in DataFrame
df1 = df.assign(Grade = ['A','B','C','D'])

# Display the DataFrame
print(df1)

Output:

     Names  Marks
0    Harry     95
1     John     85
2  Hussain     77
3    Messi     60
     Names  Marks Grade
0    Harry     95     A
1     John     85     B
2  Hussain     77     C
3    Messi     60     D

Method 4: df.join( )

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict = {'Names':['Harry', 'John', 'Hussain', 'Messi'],
         'Marks':[95, 85, 77, 60]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(dict)

# Display the Original DataFrame
print(df)

# New Column
column = pd.DataFrame({'Grade': ['A','B','C','D']})

# Add a column in DataFrame
df1 = df.join(column)

# Display the DataFrame
print(df1)

Output:

     Names  Marks
0    Harry     95
1     John     85
2  Hussain     77
3    Messi     60
     Names  Marks Grade
0    Harry     95     A
1     John     85     B
2  Hussain     77     C
3    Messi     60     D

Method 5: pd.concat( )

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict = {'Names':['Harry', 'John', 'Hussain', 'Messi'],
         'Marks':[95, 85, 77, 60]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(dict)

# Display the Original DataFrame
print(df)

# New Column
column = pd.DataFrame({'Grade': ['A','B','C','D']})

# Add a column in DataFrame
df1 = pd.concat([df, column], axis=1)

# Display the DataFrame
print(df1)

Output:

     Names  Marks
0    Harry     95
1     John     85
2  Hussain     77
3    Messi     60
     Names  Marks Grade
0    Harry     95     A
1     John     85     B
2  Hussain     77     C
3    Messi     60     D

Free Learning Resources

Leave a Comment

Your email address will not be published.