Pandas – Get First Row Value of a Given Column

To get the value of the first row of a given column use pandas.DataFrame.iloc[] property . In this article, I will explain how to get the first row and nth row value of a given column (single and multiple columns) from pandas DataFrame with Examples.

1. Quick Examples to Get First Row Value of Given Column

Below are some quick examples of how to get first row values of the given column in pandas DataFrame.


# Get first row of a given column.
df['Courses'].iloc[0]

# Get First row values of Multiple Column.
df[['Courses','Fee']].iloc[0]

# Get Nth row value of given column.
df[['Courses','Fee']].iloc[2]

# Using column Index
df.iloc[0,0]
df.iloc[1,2]

# Using row label & column label
df.loc["r2","Fee"]

Now, let’s create a Pandas DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names CoursesFeeDuration, and Discount.


# Create a Pandas DataFrame.
import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

Yields below output.


     Courses  Fee   Duration   Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      1200
r4   pandas  30000   50days      2000

2. Get First Row Value of a Given Column in Pandas DataFrame

Pandas.DataFrame.iloc[] is used to get the value of any cell by providing a row and column index. I will use this to get the first-row value of any given column. The below example gets first row value of column Courses.


# To get first row of a given column.
df['Courses'].iloc[0]
print(df)

Yields below output.


'Spark'

You can also get the same just by using iloc[] or loc[] as it takes both row and column and gets you the value of the cell.


# Using row label & column label
df.loc["r2","Fee"]

# Using column Index
df.iloc[0,0]
df.iloc[1,2]
</code.</pre>
<!-- /wp:html -->

<!-- wp:heading -->
<h2>3. Get First Row Value of Multiple Column</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Similarly, you can also get the first row values of a multiple-columns from the DataFrame using iloc[] property.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<pre><code class="language-python">
# To get First row values of Multiple Column.
df[['Courses','Fee']].iloc[0]
print(df)

Yields below output.


Courses    Spark
Fee        20000
Name: r1, dtype: object

4. Get Nth Row Value of Given Column

By now you would know that using the same approach you can get the Nth row values of given column in pandas DataFrame. Below example returns for 3rd row. (index starts from zero)


# To get Nth row value of given column.
df[['Courses','Fee']].iloc[2]

Yields below output.


Courses    Python
Fee         22000
Name: r3, dtype: object

 5. Complete Example For Get First Row Value of a Given Column


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

# To get first row of a given column.
df['Courses'].iloc[0]
print(df)

# To get First row values of Multiple Column.
df[['Courses','Fee']].iloc[0]
print(df)

# To get Nth row value of given column.
df[['Courses','Fee']].iloc[2]
print(df)

# Using column Index
print(df.iloc[0,0])
print(df.iloc[1,2])

# Using row label & column label
print(df.loc["r2","Fee"])

Conclusion

In this article, you have learned how to get the first row of a given single column, multiple columns and for N th row of selected columns with examples.

You May Also Like

References

Leave a Reply

Pandas – Get First Row Value of a Given Column