Add a new column to a dataframe

You can add a new column to a pandas DataFrame using the assign method or by directly assigning a value to a new column.

Here is an example of how to do it:

import pandas as pd

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

# Add a new column using assign method
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

In this example, we are adding a new column C with values [7, 8, 9].

Alternatively, you can also add a new column by directly assigning a value to it:

import pandas as pd

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

# Add 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 this example, we are adding a new column C with values [7, 8, 9] by directly assigning it to the DataFrame.