How to retrieve the first column of a Pandas DataFrame?

How to retrieve the first column of a Pandas DataFrame?

To retrieve the first column of a Pandas DataFrame, use the syntax “dataframe_name[‘column_name’]” where “dataframe_name” is the name of the DataFrame and “column_name” is the name of the desired column. This will return a Series object containing the values from the first column of the DataFrame. Alternatively, you can use the index location of the column by using “iloc[0]” after the DataFrame name. This will return the first column as a Series object.

Get First Column of Pandas DataFrame (With Examples)


You can use the following syntax to get the first column of a pandas DataFrame:

df.iloc[:, 0]

The result is a pandas Series. If you’d like the result to be a pandas DataFrame instead, you can use the following syntax:

df.iloc[:, :1]

The following examples show how to use this syntax in practice.

Example 1: Get First Column of Pandas DataFrame (Return a Series)

The following code shows how to get the first column of a pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
1      12        7         8
2      15        7        10
3      14        9         6
4      19       12         6
5      23        9         5
6      25        9         9
7      29        4        12

#get first column
first_col = df.iloc[:, 0]

#view first columnprint(first_col)

0    25
1    12
2    15
3    14
4    19
5    23
6    25
7    29
Name: points, dtype: int64

The result is a pandas Series:

#check type of first_colprint(type(first_col))

<class 'pandas.core.series.Series'>

Example 2: Get First Column of Pandas DataFrame (Return a DataFrame)

The following code shows how to get the first column of a pandas DataFrame and return a DataFrame as a result:

#get first column (and return a DataFrame)
first_col = df.iloc[:, :1]

#view first columnprint(first_col)

   points
0      25
1      12
2      15
3      14
4      19
5      23
6      25
7      29

#check type of first_col
print(type(first_col))

<class 'pandas.core.frame.DataFrame'>

Cite this article

stats writer (2024). How to retrieve the first column of a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-retrieve-the-first-column-of-a-pandas-dataframe/

stats writer. "How to retrieve the first column of a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-to-retrieve-the-first-column-of-a-pandas-dataframe/.

stats writer. "How to retrieve the first column of a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-to-retrieve-the-first-column-of-a-pandas-dataframe/.

stats writer (2024) 'How to retrieve the first column of a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-retrieve-the-first-column-of-a-pandas-dataframe/.

[1] stats writer, "How to retrieve the first column of a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How to retrieve the first column of a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top