Altering table means to change the existing entries or columns in dataframe or simply to modify the datarame. There are various functions and tricks by which we can alter the data in a dataframe some of them we will discuss in this section.
The common modification that we do on dataframes are :
- Add columns
- Delete columns
- Replace column contents
we can rename the columns by using the rename function which takes the old name and new name as the parameter. for example,
You can add a new column to the dataframe by using [ ] operator and entering a new column name in the operator and assigning an array-like object to it ( list, ndarray, Series).
If the column that is passed already exist then it will modify the value of that column if the number of entries in that column is the same as the number of entries passes as an array.
Any column can be deleted by using del keyword. Columns can also be deleted by using the drop function of pandas dataframe and passing the name of the columns that you want to delete. All the columns that are passed will be deleted.
Rows can also be deleted by drop function by just passing the name of the indexes as a list and passing 0 as the value of the parameter to the axis.
Insert() method of dataframe can be used to insert a column in the dataframe at a specific position. It takes 4 arguments.
- location, where you want, insert the column.
- column name.
- column values.
- allow duplicates if the column name already exists.
It Raises a ValueError if the column is already contained in the DataFrame unless allow_duplicates is set to True.
Rearranging column is basically a trick and can be done in many ways two of them have been showed in the code.
In the first method, the second column has been moved forward by simple indexing and selecting and in the second method simply the names of the column have been interchanged.
Replace column contents
you can use the [ ] operator to assign new entries to the existing column it would replace the existing entries or values and assign the new values that have been given.
So far we have been studying how to alter the column entries now let us see how to alter the whole dataframe.
we can use update method pandas dataframe to update the dataframe it will update the existing dataframe by a dataframe that is passed as an argument which takes 3 main arguments.
- other ( dataframe by which we want to update)
- join (the type of join i.e. left, right, outer, inner)
- overwrite ( if true will replace the entire dataframe)
Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length). The index can replace the existing index or expand on it.
We can pass any Python, Numpy or Pandas datatype to change all columns of a dataframe to that type, or we can pass a dictionary having column names as keys and datatype as values to change the type of selected columns.
Every string columns are treated as an object by dataframe.