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 pandas article, we can see 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 how to get 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 create DataFrame from 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(df)

Yields below output.


   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Hadoop  26000   35days      1200
3   Python  22000   40days      2500
4  PySpark  24000   60days      2000

3. Get 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 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(df2)

Yields below output.


# Output
0      Spark
1    PySpark
2     Hadoop
3     Python
4    PySpark
Name: Courses, dtype: object

4. Use df[] to Get 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(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(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(df2)

Yields below output.


# Output
   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)

7. Conclusion

In this article, I have explained how to get first column of pandas DataFrame as a Series using DataFrame.iloc[], DataFrame.columns[], and head() function with examples.

Happy Learning !!

References

Leave a Reply

You are currently viewing Pandas Get First Column of DataFrame as Series?