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.
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.
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
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')
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 !!
Related Articles
- Convert GroupBy output from Series to DataFrame
- Pretty Print Pandas DataFrame or Series
- Convert Pandas Series to NumPy Array
- Create a Set From a Series in Pandas
- Remove NaN From Pandas Series
- Pandas Series filter() Function
- Pandas Series loc[] Function
- Pandas Iterate Over Series
- Pandas explode multiple columns of DataFrame