Page 73 - Informatics_Practices_Fliipbook_Class12
P. 73
Dropping Columns
In the DataFrame df, it would make sense to drop the column Month_df2. We can drop a column using the DataFrame
method drop(), as shown below:
>>> df.drop(['Month_df2'], axis=1)
output:
Month Rent Utilities Groceries Salary Bonus Investments
0 January 1200 150 300 4000 500 1000
1 February 1200 170 350 4000 600 900
2 March 1200 160 320 4000 550 1100
3 April 1300 165 350 4000 600 900
1. How many columns does the DataFrame df have? Hint: Note that the method drop() as used
above does not modify the original DataFrame df.
2. On execution of the following function call, how many columns will the DataFrame df have?
df.drop([Month_df2], axis=1, inplace=True)
Dropping Rows from a DataFrame
Suppose, we wish to delete records for month 'January' and 'April', we may again use the drop the rows using the
DataFrame method drop() by setting the column indexes to [0, 3] and keyword argument axis to 0 as shown
below:
>>> df.drop([0,3], axis=0)
output:
Month Rent Utilities Groceries Salary Bonus Investments
1 February 1200 170 350 4000 600 900
2 March 1200 160 320 4000 550 1100
Recall that as the row axis is the default axis, to delete the rows [0, 3], we could have also used the function
call: df.drop([0,3]).
2.8.2 Renaming Columns of a DataFrame
As names of the columns of DataFrame are often read input from a spreadsheet, it may be necessary to rename some
columns to align with the data analysis task being handled.
For example, suppose, we wish to rename the column Product as Item. The rename() method in Pandas is used
to rename columns. It allows us to specify new names for one or more columns using a dictionary.
The syntax for using the rename() method to rename columns is as follows:
df.rename(columns={'current_name': 'new_name'}, inplace=True)
>>> groceryDF.rename(columns = {'Product': 'Item'}, inplace = True)
>>> groceryDF.head(3)
output:
Item Category Price Quantity
0 Bread Food 20 2
1 Milk Food 60 5
2 Biscuit Food 20 2
Another way to rename columns in a Pandas DataFrame is by specifying the complete list of names of the columns,
retaining some of the column names as it is while modifying some others, and assigning it to attribute columns of
groceryDF, as shown below:
>>> groceryDF.columns = ['Item Name', 'Category', 'Price', 'Quantity']
>>> print(groceryDF.head())
Item Name Category Price Quantity
0 Bread Food 20 2
Data Handling using Pandas DataFrame 59

