Python Pandas Data Frame Basics. Let us understand the basics of Pandas... | by Tanu N Prabhu | Towards Data Science

Before getting started let me introduce you to Pandas, Pandas is a python library that provides high-performance, easy-to-use data structures such as a series, Data Frame, and Panel for data analysis tools for Python programming language. Moreover, Pandas Data Frame consists of main components, the data, rows, and columns. To use the pandas' library and its data structures, all you have to do is to install it and import it. See the documentation of the Pandas library for a better understanding and installing guidance. Here the entire code can be found on my GitHub page.
Creating a Data Frame.Performing operations on Rows and Columns.Data Selection, addition, deletion.Working with missing data.Renaming the Columns or Indices of a DataFrame.
Creating a Data Frame.
Performing operations on Rows and Columns.
Data Selection, addition, deletion.
Working with missing data.
Renaming the Columns or Indices of a DataFrame.
The Pandas data frame can be created by loading the data from the external, existing storage like a database, SQL, or CSV files. But the Pandas Data Frame can also be created from the lists, dictionary, etc. One of the ways to create a pandas data frame is shown below:
# import the pandas libraryimport pandas as pd# Dictionary of key pair values called datadata = {'Name':['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh'],        'Age': [24, 23, 22, 19, 10]}data{'Age': [24, 23, 22, 19, 10],  'Name': ['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh']}# Calling the pandas data frame method by passing the dictionary (data) as a parameterdf = pd.DataFrame(data)df
Data Frame is a two-dimensional data structure, data is stored in rows and columns. Below we can perform some operations on Rows and Columns.
Selecting a Column: In order to select a particular column, all we can do is just call the name of the column inside the data frame.
# import the pandas libraryimport pandas as pd# Dictionary of key pair values called datadata = {'Name':['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh'],        'Age': [24, 23, 22, 19, 10]}data{'Age': [24, 23, 22, 19, 10],  'Name': ['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh']}# Calling the pandas data frame method by passing the dictionary (data) as a parameterdf = pd.DataFrame(data)# Selecting columndf[['Name']]
Selecting a Row: Pandas Data Frame provides a method called “loc” which is used to retrieve rows from the data frame. Also, rows can also be selected by using the “iloc” as a function.
# Calling the pandas data frame method by passing the dictionary (data) as a parameterdf = pd.DataFrame(data)# Selecting a rowrow = df.loc[1]rowName    Tanu Age       23 Name: 1, dtype: object
To select a particular column, all we can do is just call the name of the column inside the data frame. As seen above to work with the “loc” method you have to pass the index of the data frame as a parameter. The loc method accepts only integers as a parameter. So in the above example, I wanted to access “Tanu” row, so I passed the index as 1 as a parameter. Now there's a quick assignment for you guys, use the “iloc” method and tell me the result.
You can treat a DataFrame semantically like a dictionary of like-indexed Series objects. Getting, setting, and deleting columns works with the same syntax as the analogous dictionary operations:
# import the pandas libraryimport pandas as pd# Dictionary of key pair values called datadata = {'Name':['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh'],        'Age': [24, 23, 22, 19, 10]}# Calling the pandas data frame method by passing the dictionary (data) as a parameterdf = pd.DataFrame(data)# Selecting the data from the columndf['Age']0    24 1    23 2    22 3    19 4    10 Name: Age, dtype: int64
Columns can be deleted like with a dictionary just use the del operation.
del df[‘Age’]df
Data can be added by using the insert function. The insert function is available to insert at a particular location in the columns:
df.insert(1, ‘name’, df[‘Name’])df
Missing data occur a lot of times when we are accessing big data sets. It occurs often like NaN (Not a number). In order to fill those values, we can use “isnull()” method. This method checks whether a null value is present in a data frame or not.
Checking for the missing values.
# importing both pandas and numpy librariesimport pandas as pdimport numpy as np# Dictionary of key pair values called datadata ={‘First name’:[‘Tanu’, np.nan], ‘Age’: [23, np.nan]}df = pd.DataFrame(data)df
# using the isnull() functiondf.isnull()
The isnull () returns false if the null is not present and true for null values. Now we have found the missing values, the next task is to fill those values with 0 this can be done as shown below:
df.fillna(0)
To give the columns or the index values of your data frame a different value, it’s best to use the .rename() method. Purposefully I have changed the column name to give a better insight.
# import the pandas libraryimport pandas as pd# Dictionary of key pair values called datadata = {‘NAMe’:[‘Ashika’, ‘Tanu’, ‘Ashwin’, ‘Mohit’, ‘Sourabh’], ‘AGe’: [24, 23, 22, 19, 10]}# Calling the pandas data frame method by passing the dictionary (data) as a parameterdf = pd.DataFrame(data)df
newcols = { ‘NAMe’: ‘Name’, ‘AGe’: ‘Age’ }# Use `rename()` to rename your columnsdf.rename(columns=newcols, inplace=True)df
# The values of new indexnewindex = { 0: ‘a’, 1: ‘b’, 2: ‘c’, 3: ‘d’, 4: ‘e’}# Rename your indexdf.rename(index=newindex)
Hence above are the very important techniques or methods of Pandas data frame in Python. Some of the examples were referred from GeeksforGeeks. I have written this in a simple way such that everybody can understand and master the concepts of the data frame in Python. If you guys have some doubts about the code, the comment section is all yours.