Printing the First 10 Rows of a Pandas DataFrame
Pandas is a powerful and widely used data manipulation library in Python. It provides data structures and functions to efficiently analyze and manipulate structured data. One common task when working with data is to inspect the first few rows of a DataFrame, which can give us an initial understanding of the data's structure and contents. In this article, we will explore different ways to print the first 10 rows of a Pandas DataFrame.
Before we dive into the code, let's understand what a DataFrame is. A DataFrame is a two-dimensional labeled data structure with columns of potentially different types. It is similar to a table in a relational database or a spreadsheet. Pandas provides us with several ways to create a DataFrame, such as reading data from a CSV file, querying a database, or even creating it manually.
To print the first 10 rows of a Pandas DataFrame, we can use the `head()` function. The `head()` function returns the specified number of rows from the beginning of the DataFrame. By default, it returns the first five rows, but we can pass an integer argument to specify the desired number of rows. To print the first 10 rows, we would call `df.head(10)`, where `df` is the name of our DataFrame.
```python
import pandas as pd
# Create a DataFrame
data = {'Name': ['John', 'Alice', 'Bob', 'Emily', 'Michael', 'Sarah', 'David', 'Olivia', 'Daniel', 'Sophia'],
'Age': [25, 32, 19, 43, 50, 28, 36, 31, 27, 39],
'City': ['New York', 'Paris', 'London', 'Tokyo', 'Sydney', 'Los Angeles', 'Berlin', 'Toronto', 'Moscow', 'Rome']}
df = pd.DataFrame(data)
# Print the first 10 rows
print(df.head(10))
```
This code snippet creates a DataFrame with three columns: Name, Age, and City. The values for each column are stored in separate lists within a dictionary. We then pass the dictionary to the `pd.DataFrame()` function to create the DataFrame. Finally, we call `df.head(10)` to print the first 10 rows.
Another way to achieve the same result is by using array slicing. We can use the slicing operator `[:]` to select the desired range of rows. To print the first 10 rows, we would call `df[:10]`.
```python
import pandas as pd
# Create a DataFrame
data = {'Name': ['John', 'Alice', 'Bob', 'Emily', 'Michael', 'Sarah', 'David', 'Olivia', 'Daniel', 'Sophia'],
'Age': [25, 32, 19, 43, 50, 28, 36, 31, 27, 39],
'City': ['New York', 'Paris', 'London', 'Tokyo', 'Sydney', 'Los Angeles', 'Berlin', 'Toronto', 'Moscow', 'Rome']}
df = pd.DataFrame(data)
# Print the first 10 rows
print(df[:10])
```
Both methods will produce the same output, which will be the first 10 rows of the DataFrame:
```
Name Age City
0 John 25 New York
1 Alice 32 Paris
2 Bob 19 London
3 Emily 43 Tokyo
4 Michael 50 Sydney
5 Sarah 28 Los Angeles
6 David 36 Berlin
7 Olivia 31 Toronto
8 Daniel 27 Moscow
9 Sophia 39 Rome
```
Printing the first few rows of a Pandas DataFrame is a useful technique to quickly understand the data's structure and values. It allows us to check if the data is loaded correctly and to identify any potential issues or inconsistencies. By using the `head()` function or array slicing, we can easily achieve this task and gain valuable insights into our data.
In conclusion, Pandas provides us with efficient and straightforward methods to print the first 10 rows of a DataFrame. Whether we use the `head()` function or array slicing, these techniques allow us to explore the initial portion of our data, enabling us to make informed decisions and analysis.
Quest'articolo è stato scritto a titolo esclusivamente informativo e di divulgazione. Per esso non è possibile garantire che sia esente da errori o inesattezze, per cui l’amministratore di questo Sito non assume alcuna responsabilità come indicato nelle note legali pubblicate in Termini e Condizioni
Quanto è stato utile questo articolo? 0Vota per primo questo articolo!