Print a specific row of a pandas DataFrame
source link: https://thispointer.com/print-a-specific-row-of-a-pandas-dataframe/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
In this article, we will discuss different ways to print a specific row of a pandas DataFrame.
Table of Contents
Preparing DataSet
To quickly get started, let’s create a sample dataframe to experiment. We’ll use the pandas library with some random data.
import pandas as pd # List of Tuples employees= [('Shubham', 'India', 'Tech', 5, 4), ('Riti', 'India', 'Design' , 7, 7), ('Shanky', 'India', 'PMO' , 2, 2), ('Shreya', 'India', 'Design' , 2, 0), ('Aadi', 'US', 'PMO', 11, 5), ('Sim', 'US', 'Tech', 4, 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Location', 'Team', 'Experience', 'RelevantExperience'], index = ['A', 'B', 'C', 'D', 'E', 'F']) print(df)
Contents of the created dataframe are,
Name Location Team Experience RelevantExperience A Shubham India Tech 5 4 B Riti India Design 7 7 C Shanky India PMO 2 2 D Shreya India Design 2 0 E Aadi US PMO 11 5 F Sim US Tech 4 4
Print specific row of DataFrame based on index position
In a pandas DataFrame, iloc
is used to access any row or column based on their position. Let’s understand with an example, say, we need to print the row number 3 of the above DataFrame.
# print row 3 print (df.iloc[2])
Output
Name Shanky Location India Team PMO Experience 2 RelevantExperience 2 Name: C, dtype: object
As observed, it has printed all the values along with their column header of row number 3. In case, we need only values, we can simply use the .values property here as below.
# print row 3 print (df.iloc[2].values)
Output
['Shanky' 'India' 'PMO' 2 2]
Print specific row of DataFrame based on index name
Another approach is to print any specific row based on the row name. Here, we will use the loc
property of the pandas DataFrame.
# print row with name 'C' print (df.loc['C'])
Output
Name Shanky Location India Team PMO Experience 2 RelevantExperience 2 Name: C, dtype: object
As observed, it has returned a similar output as above. It is generally preferred when the index names are known, as calculating position might be an issue sometimes.
The complete example is as follows,
import pandas as pd # List of Tuples employees= [('Shubham', 'India', 'Tech', 5, 4), ('Riti', 'India', 'Design' , 7, 7), ('Shanky', 'India', 'PMO' , 2, 2), ('Shreya', 'India', 'Design' , 2, 0), ('Aadi', 'US', 'PMO', 11, 5), ('Sim', 'US', 'Tech', 4, 4)] # Create a DataFrame object from list of tuples df = pd.DataFrame(employees, columns=['Name', 'Location', 'Team', 'Experience', 'RelevantExperience'], index = ['A', 'B', 'C', 'D', 'E', 'F']) print(df) # print row 3 print (df.iloc[2]) # print row with name 'C' print (df.loc['C'])
Output:
Name Location Team Experience RelevantExperience A Shubham India Tech 5 4 B Riti India Design 7 7 C Shanky India PMO 2 2 D Shreya India Design 2 0 E Aadi US PMO 11 5 F Sim US Tech 4 4 Name Shanky Location India Team PMO Experience 2 RelevantExperience 2 Name: C, dtype: object Name Shanky Location India Team PMO Experience 2 RelevantExperience 2 Name: C, dtype: object
Summary
In this article, we have discussed how to print a specific row of a Pandas DataFrame. Thanks.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.
Join a LinkedIn Community of Python Developers
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK