Site icon Spark By {Examples}

Pandas Get First Row Value of a Given Column

pandas get first row

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.

Key Points –

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.


# 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.
df2=df['Courses'].iloc[0]
print(df2)

# Output:
# Spark

Alternatively, To get the first-row value of a given column in a Pandas DataFrame. Replace ‘r1’ with the actual index of the row you want, and ‘Courses’ with the name of the column you are interested in. The at method is used for quick label-based access to a single value.


# Get the first row value of the 'Courses' column
df = df.at['r1', 'Courses']
print(df)

# 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]

3. Get First Row Value of Multiple Column

Similarly, you can also get the first row values of multiple columns from the DataFrame using iloc[] property.


# To get first row values of multiple column
df2 = df[['Courses','Fee']].iloc[0]
print(df2)

# Get the first row values 
# Of multiple columns
df2 = df.iloc[0, :2]  
print(df2)

In the above example, df.iloc[0, :2] extracts the values from the first row for the first two columns (‘Courses’ and ‘Fee’). Adjust the column indices (:2) based on the columns you are interested in. This example yields the below output.


# Output:
Courses    Spark
Fee        20000
Name: r1, dtype: object

4. Get Nth Row Value of the Given Column

To get the Nth row values of specific columns in a Pandas DataFrame. 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 values of given columns
row_index = 2
selected_columns = ['Courses', 'Fee']
df2 = df[selected_columns].iloc[row_index]
print(df2)

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

This code will print the values in the specified columns (‘Courses’ and ‘Fee’) for the row with index 2. Adjust the row_index and selected_columns according to your specific needs. Yields below output.


# 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"])

Frequently Asked Questions on Get First Row Value of a Given Column

How do I get the first row value of a specific column in a Pandas DataFrame?

To get the first row value of a specific column in a Pandas DataFrame, you can use various methods, including iloc, loc, or direct attribute access.

Can I use loc to achieve the same result?

You can use the loc method to achieve the same result for getting the first row value of a specific column in a Pandas DataFrame.

Is there a way to directly access a column as an attribute?

You can directly access a column as an attribute in a Pandas DataFrame. This is a convenient and concise way to retrieve column values. For example, replace ‘Column_Name’ with the actual name of the column you want to access. This syntax treats the column as an attribute of the DataFrame, and you can then use the .iloc[0] to get the value in the first row.

How can I get the first row values of multiple columns?

To get the first row values of multiple columns in a Pandas DataFrame, you can use the iloc method with the column indices.

What if I want to get the Nth row value of a given column?

If you want to get the Nth row value of a specific column in a Pandas DataFrame, you can use similar methods.
Using iloc (Integer Location), and Using loc (Label-based Location).

Are there other efficient methods like at or iat for single value access?

You can use at for label-based access and iat for integer position access. These methods (at and iat) are more efficient than loc and iloc when you’re accessing a single value in a DataFrame. They are particularly useful when dealing with large datasets or when you need to perform repeated single-value lookups.

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.

References

Exit mobile version