Get columns from Pandas DataFrame | using columns, values, info, sorted methods
In this chapter of pandas tutorial, we will discuss:
-
create dataframe in pandas,
-
get column names from pandas dataframe using columns method,
-
how to get columns from pandas dataframe using values on columns,
-
get columns from pandas dataframe using info(),
-
how to get sorted column in pandas,
Create DataFrame
DataFrame is a 2 dimensional data structure that stores data in 2 dimensional format. One dimension refers to a row and second dimension refers to a column. So it will store data in rows and columns.
We can create DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe. The data can be a dictionary that stores list of values with specified key
Example: create dataframe pandas
In this example, we will create dataframe in pandas with 4 rows and 4 columns with college data
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
})
#display the dataframe
print(data)
Output: Below dataframe is created
college_id college_name college_address Total Staff
0 c-001 vignan university guntur 1200
1 c-021 vvit guntur 3422
2 c-002 RVR - JC guntur 5644
3 c-004 Andhra University guntur 670
Lets start to get columns from pandas dataframe. We will see different methods to do that task.
Method 1 - get columns from pandas dataframe using columns method
This columns is used to return or get columns from pandas dataframe in a list. It will also return the column datatype i.e object along with column name.
Syntax:
dataframe.columns
where, dataframe is the input dataframe
Example: get column names from pandas dataframe using columns method is shown in below code snippet
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
})
#display the columns in the dataframe
print(data.columns)
Output: We get column names from pandas dataframe below
Index(['college_id', 'college_name', 'college_address', 'Total Staff'], dtype='object')
Lets see another method to get column names from pandas dataframe.
Method 2 : get columns from pandas dataframe using columns.values
This columns.values is used to return the column names in a list without datatype.
Syntax:
dataframe.columns.values
where, dataframe is the input dataframe
Example: extract column names from pandas dataframe using values
In this example, we are using shape function to return number of rows and columns in a tuple from the dataframe.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
})
#display the columns in the dataframe
print(data.columns.values)
Output: we successfully extract column names from pandas dataframe
['college_id' 'college_name' 'college_address' 'Total Staff']
Lets see another method to get columns from pandas dataframe.
Method 3 : get columns from pandas dataframe using sorted() function
This sorted() function is used to return the column names in a list without datatype in sorted order.
Syntax:
sorted(dataframe)
where, dataframe is the input dataframe
Example: Lets see how to get sorted column in pandas from below snippet:
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
})
#display the columns in the dataframe
print(sorted(data))
Output:
['Total Staff', 'college_address', 'college_id', 'college_name']
You can see the sorting is done on columns.
Method 4 : get columns from pandas dataframe using info() function
This info() method will return the information about column. The information includes:
-
column names with associated datatypes and
-
count of Not Null values
Syntax:
dataframe.info()
where, dataframe is the input dataframe
Example: Lets get get columns from pandas dataframe with all information
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":[1200,3422,5644,670]
})
#display the columns in the dataframe
print(data.info())
Output: We get columns from pandas dataframe as below
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 college_id 4 non-null object
1 college_name 4 non-null object
2 college_address 4 non-null object
3 Total Staff 4 non-null int64
dtypes: int64(1), object(3)
memory usage: 256.0+ bytes
None
Thus we get other details related to columns as well using info().
Conclusion
In this article, we first create dataframe in pandas and get columns from pandas dataframe explained in four ways - columns, values, sorted, info.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Feb 24,2022