Built with Tutify.io

    AIStudyLabs

    Pandas Get First Column of DataFrame as Series?

    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.

    Key Points –

    <a id="h-quick-examples-of-getting-first-column-as-a-series"></a>

    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
    

    <a id="h-create-pandas-dataframe"></a>

    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.

    <a id="h-get-the-first-column-as-a-series"></a>

    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.

    <a id="h-use-df-to-get-the-first-column-as-a-series"></a>

    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.

    <a id="h-use-head-to-get-first-column-of-pandas-dataframe"></a>

    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
    

    <a id="h-complete-example"></a>

    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)
    

    <a id="h-frequently-asked-questions-of-get-first-column-of-dataframe-as-series"></a>

    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'] or
    df_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')

    <a id="h-conclusion"></a>

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

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References