Select column from a Dataframe
In this exercise, we are using the datasource employees.csv. You can download the datasource and use for the transformation.
We can select a particular column of the dataframe by using the square brackets [] with the column name, which we want to select.
Syntax df[“column_name”]
Example: Here, we are extracting the “Company” column from the dataframe.
Python
comp=mydata["Company"] comp
The output of the above code is shown below:
Note: Pandas extract the column from the dataframe as a series. If multiple columns are extracted, it is extracted as a dataframe obviously.
Example: Here, we are extracting the “Name” and “Company” columns from the dataframe.
Python
comp=mydata[["Name", "Company"]] comp
Or, we can rewrite the code as shown in the following:
Python
select_columns= ["Name", "Company"] comp=mydata[select_columns] comp
The output of the above code is shown below: