We can get the first column of pandas DataFrame as a Series by using iloc[]
, columns[]
, and head()
function. In this article, I will explain how to get the first columns of DataFrame as a series with several examples.
Key Points –
- Use the
.iloc
method to access the first column by index position. - You can retrieve the first column using positional indexing, such as
df.iloc[:, 0]
. - The
.iloc
property allows precise row and column selection by integer-location, making it straightforward to get the first column. - If you know the column name, it can be directly accessed with
.loc
or by callingdf['column_name']
. - Extracting a single column from a DataFrame returns a Series, not a DataFrame.
- Using
.columns[0]
withdf[]
allows retrieval of the first column by its name dynamically.
Quick Examples of Getting First Column as a Series
If you are in a hurry below are some quick examples of getting the first column as a Series.
# Quick examples of getting first column as a series
# Example 1: Use Dataframe.iloc[]
# To get first column as a series
df2 = df.iloc[:, 0]
# Example 2: Use columns[]
# To get first column as a series
df2 = df[df.columns[0]]
# Example 3: Use column name
# To get first column as a series
df2 = df.Courses
# Example 4: Use head() function
# To get first column as a series
df2 = df.T.head(1).T
Create Pandas DataFrame
Let’s create a Pandas DataFrame from a Python dictionary in which keys
are 'Courses'
, 'Fee'
, 'Duration'
and 'Discount‘
, and values are taken as a list of corresponding key values.
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Hadoop","Python","PySpark"],
'Fee' :[20000,25000,26000,22000,24000],
'Duration':['30days','40days','35days','40days','60days'],
'Discount':[1000,2300,1200,2500,2000]
}
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)
Yields below output.
Get the First Column as a Series
In pandas, each column is represented as a Series hence it is very easy to get the first column of pandas DataFrame as a Series by using the iloc[]
property. Use df.iloc[:,0]
to get the first column as a Series. For example.
# Get first column as a series
df2 = df.iloc[:, 0]
print("After getting the first column of DataFrame as a series:\n", df2)
Yields below output.
Use df[] to Get the First Column as a Series
When we use df[df.columns[i]]
function for extracting the first column of DataFrame, it will return the column based on the label associated with the index. Here, df.columns[0]
returns the label of the first column of DataFrame, and df['label']
returns the column as a Series.
# Get first column as a series
df2 = df[df.columns[0]]
print("After getting the first column of DataFrame as a series:\n", df2)
Yields the same output as above. We can also use the column name to extract the first column as a series. For examples.
# Use column name
# To get first column as a series
df2 = df.Courses
print("After getting the first column of DataFrame as a series:\n", df2)
Yields the same output as above.
Use head() to Get First Column of Pandas DataFrame
We can also use df.T.head(1).T
to get the first column of pandas DataFrame as a Series.
# Use head() function
# To get first column as a series
df2 = df.T.head(1).T
print("After getting the first column of DataFrame as a series:\n", df2)
Yields below output.
# Output:
# After getting the first column of DataFrame as a series:
Courses
0 Spark
1 PySpark
2 Hadoop
3 Python
4 PySpark
Complete Example
import pandas as pd
technologies = {
'Courses':["Spark","PySpark","Hadoop","Python","PySpark"],
'Fee' :[20000,25000,26000,22000,24000],
'Duration':['30days','40days','35days','40days','60days'],
'Discount':[1000,2300,1200,2500,2000]
}
df = pd.DataFrame(technologies)
print(df)
# Use DataFrame.iloc[]
# To get first column as a series
df2 = df.iloc[:, 0]
print(df2)
# Use columns[]
# To get first column as a series
df2 = df[df.columns[0]]
print(df2)
# Use column name
# To get first column as a series
df2 = df.Courses
print(df2)
# Use head() function
# To get first column as a series
df2 = df.T.head(1).T
print(df2)
Frequently Asked Questions of Get First Column of DataFrame as Series
To get the first column as a Series, you can use square bracket indexing with the column name or the .iloc
method. For example, df_first_column = df['ColumnName']
ordf_first_column = df.iloc[:, 0]
If you don’t know the name of the first column, you can use the .iloc
method with the numeric index. For example, df_first_column = df.iloc[:, 0]
You can use the .iloc
method with the numeric index to get the first column. For example, df_first_column = df.iloc[:, 0]
If you want to specify the data type of the resulting Series, you can use the .astype()
method. For example, df_first_column = df['ColumnName'].astype('desired_dtype')
Conclusion
In this article, I have explained how to get the first column of pandas DataFrame as a Series by using DataFrame.iloc[]
, DataFrame.columns[]
, and head()
function with examples.
Happy Learning !!
Related Articles
- Pandas Iterate Over Series
- Pandas Series rank() Function
- Pandas Series loc[] Function
- Pandas Series filter() Function
- Pandas Series mode() Function
- Remove NaN From Pandas Series
- Pandas Series.rolling() Function
- Create a Set From a Series in Pandas
- Pretty Print Pandas DataFrame or Series
- Convert Pandas Series to NumPy Array
- Pandas Series.str.contains() With Examples
- Pandas explode multiple columns of DataFrame
- Convert GroupBy output from Series to DataFrame