Add a column to a Pandas DataFrame.
To add a column to a Pandas DataFrame, you can use the assign
method. For example, suppose you have a DataFrame called df
and you want to add a column called new_column
with values from a list new_values
. You can do this by using the following code:
This code creates a new DataFrame with the same data as df
, but with an additional column called new_column
that contains the values from new_values
. If the length of new_values
is the same as the number of rows in df
, then each value in new_values
will be assigned to the corresponding row in the new column.
Alternatively, you can use the insert
method to add a column to a DataFrame at a specific index. For example, suppose you want to insert the new_column
at the third position in the DataFrame. You can do this by using the following code:
This code will insert the new_column
at the third index in the DataFrame, and it will overwrite any existing data at that index. The True
argument specifies that you want to insert the column in place, without creating a new DataFrame.
It's important to note that both the assign
and insert
methods return new DataFrames, so you will need to use them in an assignment statement to actually add the new column to the original DataFrame.
Here's an example of how you can use these methods to add a new column to a DataFrame:
import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6]
})
# create a list of values for the new column
new_values = [7, 8, 9]
# use the assign method to add a new column
df = df.assign(C = new_values)
# display the DataFrame
print(df)
# insert the new column at the third position
df.insert(3, "D", new_values, True)
# display the updated DataFrame
print(df)
The output of this code will be a DataFrame with two additional columns: C
and D
, which contain the values from new_values
.