• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:11 mins read
You are currently viewing Pandas Get First Column of DataFrame as Series?

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.

1. 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.


# Below are quick examples.

# 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

2. 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.

pandas first column series

3. 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.

pandas first column series

4. 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.

5. 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

6. 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

How do I get the first column of a DataFrame as a Series in pandas?

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'] or
df_first_column = df.iloc[:, 0]

How do I get the first column without knowing its name?

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]

Is there a way to get the first column as a Series with just the column number?

You can use the .iloc method with the numeric index to get the first column. For example, df_first_column = df.iloc[:, 0]

How can I get the first column as a Series with a specific data type?

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')

7. 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 !!

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium