Rows and Columns in Pandas DataFrame:

Dealing with columns in Python for Data Science:

To deal with columns, we need to perform basic operations on columns such as selecting, adding, detecting, and renaming.

Rows and Column in Pandas DataFrame Python for Data Science - PST

Selection of column:

Selection of a column in Pandas DataFrame is done by calling their column names.

Rows and Column in Pandas DataFrame Python for Data Science - PST

OUTPUT:

Rows and Column in Pandas DataFrame Python for Data Science - PST

Column Addition:

For adding a column in Pandas dataframe, we have to declare a new list as column, and we need to add it to the existing dataframe.

OUTPUT:

Column deletion:

To delete column in Pandas dataframe we use the drop() method. Columns can be deleted by dropping columns with column names.

Rows and Column in Pandas DataFrame Python for Data Science - PST

OUTPUT:

Data frame before dropping columns:

Rows and Column in Pandas DataFrame Python for Data Science - PST

Data frame after dropping columns:

Rows and Column in Pandas DataFrame Python for Data Science - PST

Dealing with rows:

We can perform basic operations on rows such as selecting, adding, deleting, and renaming.

Row selection:

Pandas provides a method for retrieving rows from data frame .DataFrame.loc[] is used for retrieving rows from Pandas DataFrame. We can also select rows by passing integer location to iloc[] function.

OUTPUT:

Addition of rows:

For addition of rows in Pandas DataFrame, concatenation of old dataframe with new one is possible.

OUTPUT:

Data frame before adding row:

Data frame after adding row:

Row deletion:

For the deletion of a row in Pandas DataFrame, we use the drop() method. Rows can be deleted by dropping rows by index label.

OUTPUT:

As we see in the images below, the new output does not have passed values. These values were dropped and changes were made in original data frame since inplace was True.

Dataframe before dropping values:

Dataframe after dropping values:

So, to learn more about it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.