Append a new column to an existing dataframe python

You can append a new column to an existing DataFrame in Python using the assign method or by directly assigning a new column to the DataFrame.

Here is an example using the assign method:

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# Append a new column
df = df.assign(C=[7, 8, 9])

print(df)

Output:

   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

And here is an example of directly assigning a new column:

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# Append a new column
df['C'] = [7, 8, 9]

print(df)

Output:

   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

In both cases, the new column C is appended to the existing DataFrame df.