Commands Related to DataFrame Programming
Commands Related to DataFrame Programming
Command to add a new column at the specific index e.g. at 3rd position i.e. index 2 of the DataFrame
using insert( )
>>>Test.insert(2, ‘Class/Sec’, np.NaN)
Command to replace all the NaN values with any specified value
>>>Test.fillna( 0 )
The above command will fill 0 at all the places where there is NaN value in a DataFrame.
Attributes of DataFrame (we will use the above given dataframe Test )
Name of the Command Output
attribute
columns >>>Test.columns Index([‘Name’, ‘Test1’, ‘Test2’, ‘Test3’], dtype = ‘object’)
index >>>Test.index RangeIndex(start = 0, stop = 5, step = 1)
dtypes >>>Test.dtypes Name object
Test1 int64
Test2 int64
Test3 int64
dtype : object
(5, 4)
shape >>>Test.shape Note – first value shows number of rows and second value shows
no. of columns in a DataFrame
size >>>Test.size 20 ( Total number of elements)
values >>>Test.values [[‘Amit’ , 92, 75, 79]
[‘Yash’, 81, 85, 84]
[‘Rohan’, 76, 72, 85]
[‘Devesh’, 78, 83, 90]
[‘Nihar’, 92, 87, 87]]
head( ) – head function will display top 5 rows by default of the DataFrame. If we specify any value within
the function it will display that no. of rows from the top.
>>>Test.head( 3 )
Will show top 3 rows of the datFrame Test
tail( ) – tail function will display bottom 5 rows by default of the DataFrame. If we specify any value within
the function it will display that no. of rows from the bottom.
>>>Test.tail( 2 )
Will show bottom 2 rows of the datFrame Test