Viewing the Data
To view the first few rows of a dataset, use the head() method. This method accepts an integer argument that specifies how many rows to display (by default, it shows the first 5 rows). Display the first 10 rows of the dataset:
df = pd.read_csv('file.csv')
# Extracting the first 10 rows
first_lines = df.head(10)
To see the last few rows of a DataFrame, use the tail() method. It works the same way as the head() method:
df = pd.read_csv('file.csv')
# Extracting the last 10 rows
last_lines = df.tail(10)
Another useful method for exploring DataFrames is sample(). This method fetches random records from a DataFrame. By default, it retrieves a single random record unless specified otherwise.
df = pd.read_csv('file.csv')
# Extracting 10 random rows
random_lines = df.sample(10)
Swipe to start coding
You are given a DataFrame named wine_data.
- Extract the first 10 rows from this
DataFrameand store the result in thefirst_linesvariable. - Retrieve the last 15 rows from this
DataFrameand store the result in thelast_linesvariable. - Select a random sample of 12 rows from this
DataFrameand store the result in therandom_rowsvariable.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
How can I display more or fewer rows using these methods?
What happens if I request more rows than exist in the dataset?
Can you explain the difference between head(), tail(), and sample()?
Awesome!
Completion rate improved to 3.03
Viewing the Data
Swipe to show menu
To view the first few rows of a dataset, use the head() method. This method accepts an integer argument that specifies how many rows to display (by default, it shows the first 5 rows). Display the first 10 rows of the dataset:
df = pd.read_csv('file.csv')
# Extracting the first 10 rows
first_lines = df.head(10)
To see the last few rows of a DataFrame, use the tail() method. It works the same way as the head() method:
df = pd.read_csv('file.csv')
# Extracting the last 10 rows
last_lines = df.tail(10)
Another useful method for exploring DataFrames is sample(). This method fetches random records from a DataFrame. By default, it retrieves a single random record unless specified otherwise.
df = pd.read_csv('file.csv')
# Extracting 10 random rows
random_lines = df.sample(10)
Swipe to start coding
You are given a DataFrame named wine_data.
- Extract the first 10 rows from this
DataFrameand store the result in thefirst_linesvariable. - Retrieve the last 15 rows from this
DataFrameand store the result in thelast_linesvariable. - Select a random sample of 12 rows from this
DataFrameand store the result in therandom_rowsvariable.
Solution
Thanks for your feedback!
single