519,653 views
45 votes
45 votes
As the first step, load the dataset from airline-safety.csv by defining the load_data function below. Have this function return the following (in order):

1. The entire data frame
2. Total number of rows
3. Total number of columns
def load_data():
# YOUR CODE HERE
df, num_rows, num_cols

User James Gupta
by
2.6k points

1 Answer

28 votes
28 votes

Answer:

import pandas as pd

def load_data():

df = pd.read_csv("airline-safety.csv')

num_rows = df.shape[0]

num_cols = df.shape[1]

return df, num_rows, num_cols

Step-by-step explanation:

Using the pandas python package.

We import the package using the import keyword as pd and we call the pandas methods using the dot(.) notation

pd.read_csv reads the csv data into df variable.

To access the shape of the data which is returned as an array in the format[number of rows, number of columns]

That is ; a data frame with a shape value of [10, 10] has 10 rows and 10 columns.

To access the value of arrays we use square brackets. With values starting with an index of 0 and so on.

The row value is assigned to the variable num_rows and column value assigned to num_cols

User Al Fahad
by
3.1k points