Use case examples – get Pandas DataFrame data structure information
Once you load your data into Pandas Dataframe (by either reading a CSV, Excel file, database or creating it in Python) , you will want to check that the data is loaded correctly.
Here are some codes to address some common questions you might have about the data structure of your Pandas Dataframe.
Example Code – get Pandas DataFrame data structure information
How do I view a sample of my data in my dataframe?
View the 1st n records in your dataframe
# df is your pandas dataframe #this line allows you to view 1st 5 records in your dataframe df.head() #You can change the # of records by inputting a number within the head parenthesis #show 1st 20 rows df.head(20)
View last n records in your dataframe
# df is your pandas dataframe #this line allows you to view last 5 records in your dataframe df.tail() #You can change the # of records by inputting a number within the parenthesis #show last 20 rows df.tail(20)
Get number of rows and columns in your Pandas Dataframe
#df is your dataframe #get the number of rows and columns in your dataframe df.shape
List the column names of your Pandas Dataframe and data types of each column
You may not remember all the columns in your Panda Dataframes. The following code will help you quickly list the column names and the data type of each column.
#list column names and data types of each column #df is the name of your dataframe df.dtypes