ADVERTISEMENT
ADVERTISEMENT

How to get the first column of a pandas DataFrame as a Series?

Given a Pandas DataFrame, we have to get the first column of a pandas DataFrame as a Series.
Submitted by Pranit Sharma, on June 17, 2022

Pandas is a special tool which allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame.

Columns are the different fields which contains their particular values when we create a DataFrame. We can perform certain operations on both rows & column values.

To get the first column of pandas DataFrame as a series, we will use pandas.DataFrame.iloc property.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example,

Python code to get the first column of a pandas DataFrame as a Series

# Importing pandas package
import pandas as pd

# Creating a dictionary
d= {
    'Points':[10,20,30,15,25,35],
    'Assists':[2,2,3,3,4,4]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display DataFrame
print("Created DataFrame\n",df,"\n")

# Get First column
first_col = df.iloc[:, 0]

# Display First column
print("First column:\n",first_col)

Output:

Example: Get the first column

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.