Coding Ref

How to print a specific row in a Pandas DataFrame?

How to print a specific row in a Pandas DataFrame?

To print a specific row in a Pandas DataFrame, you can use the iloc method.

Here is an example:

main.py
import pandas as pd

# create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# print the first row
print(df.iloc[0])

# print the second row
print(df.iloc[1])
output
A    1
B    4
Name: 0, dtype: int64

A    2
B    5
Name: 1, dtype: int64

The iloc method allows you to access a row of the DataFrame by its index. In the example above, we use the index 0 to access the first row, and 1 to access the second row. You can use any valid index value to access a specific row.

Keep in mind that the index of a DataFrame starts at 0, so the first row has an index of 0, the second row has an index of 1, and so on.

You'll also like

Related tutorials curated for you

    How to groupby mean in Pnadas

    What is nlargest() in Pandas?

    What is isna() in Pandas?

    How to split a Pandas DataFrame by a column value

    How to get the first row in Pandas

    What is idxmax() in Pandas?

    What does factorize() do in Pandas?

    How to use qcut() in Pandas

    How to fix: AttributeError module 'pandas' has no attribute 'dataframe'

    What does Diff() do in Pandas?

    How to join two DataFrames in Pandas

    How to normalize a column in Pandas