Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we will learn how to add a new column to our DataFrames in Pandas. Can anyone tell me why we might want to add a new column to our dataset?
To include more information about our data!
Exactly! For example, let's say we want to add a 'Score' column to our existing DataFrame. We can do that by simply using the syntax: df['Score'] = [85, 90, 95]. This assigns scores to each row. Remember this simple phrase: 'Assigning values makes columns thrive!'
What if we want to add more scores later?
Great question! You can update the column values anytime by reassigning it. Just keep in mind, the lengths must match the number of rows in the DataFrame.
What happens if the lengths are different?
If they are different, Pandas will raise an error. Now, let's summarize: to add a column, use df['Column_Name'] = values. Make sure the number of values matches your rows!
Now, let's discuss deleting a column. Can anyone suggest how we might do this in Pandas?
Maybe we use a function to remove it?
Correct! We can use the drop() method. For example, df.drop('Score', axis=1, inplace=True) removes the 'Score' column. Does anyone remember what axis=1 indicates?
It means we are referring to a column, right?
Exactly! And inplace=True means we make the change directly to our original DataFrame. If we set inplace=False, it will return a new DataFrame without the column but won't change the original. Let's repeat: to delete a column, remember 'Drop it like it’s hot!' by using df.drop('Column_Name', axis=1, inplace=True).
Can we remove multiple columns at once?
Absolutely! Simply pass a list of column names to the drop() function, like this: df.drop(['Column1', 'Column2'], axis=1, inplace=True).
So if we wanted to remove 'Score' and 'Age' columns, we could do it all at once?
You got it! Let’s summarize: to delete a column, use df.drop('Column_Name', axis=1, inplace=True). Now, any questions before we wrap up?
Column adding is easy, just assign, no need to whine, use df['Name'] you’ll be just fine.
Imagine a farmer (DataFrame) adding crops (new columns) to his farm – every new crop must fit in the rows of his planting plan.
Remember A.D.D: Assign Data for a new column (Adding Direct Data).
C.A.D. - Create A DataFrame for adding columns!
Term
How to add a column in Pandas?
Click to flip
Term
What does axis=1 mean in df.drop()?
Click to flip
Term
What does inplace=True do?
Click to flip
A two-dimensional labeled data structure with columns of potentially different types.
To introduce a new column to a DataFrame, assigning values to it.
A method used to remove specified labels from rows or columns.
An integer that specifies whether to drop a column (1) or a row (0).
An argument that allows changes to be applied directly to the DataFrame.
This section teaches how to add and delete columns in a Pandas DataFrame.
In this section, you'll learn how to enhance your data by adding new columns and manage your data effectively by removing unnecessary ones. Both actions are crucial for data manipulation in Pandas.
The section on adding and deleting columns focuses on two fundamental operations when managing data in a DataFrame using Pandas. Adding a new column is as straightforward as assigning a list or a Series to a new column label. For example, df['Score'] = [85, 90, 95] adds a new column named 'Score'. On the other hand, removing a column can be accomplished with df.drop('Score', axis=1, inplace=True), where you specify axis=1 to indicate that you want to drop a column (as opposed to a row, which would be axis=0). The inplace=True argument ensures that the changes apply directly to the original DataFrame without needing to create a new variable. Understanding these operations is crucial for data preparation, especially in machine learning contexts.