DataFrame Attributes
All
information related to a DataFrame is available in its attributes. Python
provides a lot of dataframe attributes to access the information of a
dataframe.
We can access all
the information as below.
<DataFrame Object> . <Attribute Name>
Example
import pandas as pd
dictObj={‘EmpId’:[‘E01’,’E02’,’E03’],
‘EmpName’:[‘Raj,’Ram’,’Renu’],
‘Department’:[‘Accounts’,’HR’,’IT’]
}
df=pd.DataFrame(dictObj)
Get information related to Index,
Columns, Axes and Data Types
Index:Returns the starting value, ending value, and
the difference(step) of row index.
>>> df.index
Output is RangeIndex(start=0,stop=3,step=1)
Columns:Returns the column name of the dataframe.
>>> df.columns
Output : Index([‘EmpId’,’EmpName,’Department’],dtype=’object’)
Axes:Returns a list which contains the rowindex as well as the column name of
the dataframe.
>>> df.axes
Output : [RangeIndex(start=0,stop=3,step=1), Index([‘EmpId’,’EmpName,’Department’],dtype=’object’)]
Dtypes:Returns datatypes of each column of a dataframe.
>>> df.dtypes
Output :
EmpId Object
EmpName Object
Department Object
dtype : Object
See the output of all the above attributes in running mode.
Retrieving
size (no. of elements), shape, number of dimensions
size:Returns a total number of elements present in dataframe.
>>> df.size
Output 12
shape:Returns a tuple which gives the present number of rows and number of
columns of a dataframe as an element.
>>> df.shape
Output : (3,3)
sdim : Returns an integer value which represents the number of dimensions of a
dataframe.
>>> df.ndim
Output : 2
See the output of all the above attributes in running mode.
Retrieving
values
values:
Returns a NumPy array which contains all rows as a value.
df.values
empty: Returns a Boolean value which represents if
the dataframe is empty or not. If it will return “True”, then dataframe is
empty, otherwise, it is not empty.
>>> df.empty
Transposing a
DataFrame (T) :It transposes a dataframe, i.e., rows become
columns and columns become rows.
>>> df.T
Getting Count of non-NA values in dataframe
count(): It will
return non-NA values for each COLUMNS. By default, it will take 0 as an
argument.
>>> df.count()
Output :
EmpId 3
EmpName 3
Department 3
count(1) :If we pass 1
as an argument, then instead of returning number of columns, it will return
number of each rows along with index number,
>>> df.count(1)
Output
0 3
1 3
2 3
Count with axis parameter :We can also explicitly specify an argument to count() as axis.
If we want to count columns value then pass argument like
>>> df.count(axis=’columns’)
If we want to count rows value then pass argument like
>>> df.count(axix=’rows’)
See the output of all the above count attribute in running mode,
No comments:
Post a Comment