0% found this document useful (0 votes)
4 views

Commands Related to DataFrame Programming

The document provides commands for manipulating a DataFrame in Python using pandas. It includes instructions for creating a DataFrame, selecting columns and rows, adding and deleting data, handling missing values, and accessing DataFrame attributes. Additionally, it explains how to display the top and bottom rows of the DataFrame.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Commands Related to DataFrame Programming

The document provides commands for manipulating a DataFrame in Python using pandas. It includes instructions for creating a DataFrame, selecting columns and rows, adding and deleting data, handling missing values, and accessing DataFrame attributes. Additionally, it explains how to display the top and bottom rows of the DataFrame.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Commands related to DataFrame programming

Consider the following dataframe ‘Test’


Test
Name Test1 Test2 Test3
0 Amit 92 75 79
1 Yash 81 85 84
2 Rohan 76 72 85
3 Deves 78 83 90
h
4Nihar 92 87 87
Create a DataFrame
import pandas as pd
d = {‘Name’: [ ‘Amit’, Yash’, ‘Rohan’, ‘Devesh’, ‘Nihar’], ‘Test1’ : [92,81, 76, 78, 92], ‘Test2’ :[75, 85,
72, 83, 87], ‘Test3’ : [79, 84, 85, 90, 87]}
Test = pd.DataFrame(d)

Command to select a single column


On Interactive On Script Mode
Mode
>>> Test[‘Test1’] print(Test[‘Test1’])
or or
>>> Test.Test1 print(Test.Test1)
Command to select a multiple columns
>>>Test[[‘Name’, ‘Test3’]]

Command to select a single row using loc


>>>Test.loc[1, : ]

Command to select a multiple contiguous rows using loc


>>> Test.loc[1 : 3, : ]

Command to select a specific value e.g. Test2 marks of Rohan


>>>Test.at[2, ‘Test2’]

Command to add a new column at the end of the DataFrame


import numpy as np (required to import to use NaN datatype
>>>Test[‘Total’] = np.NaN

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 add a new row at the end of the DataFrame


>>> Test.loc[ 5, : ] = np.NaN

Command to delete a single column


>>> del Test[‘Total’]

Command to delete a row using drop( )


>>> Test.drop(index = 2, inplace = True)
Note – drop function make the changes temporary. To make the changes permanent inplace attribute in used.
Command to delete a column using drop( )
>>> Test.drop(columns = ‘Total’, inplace = True)

Command to find any missing value in the DataFrame


>>>Test.isnull( )
Note – This function will return True or False for each value of DataFrame.

Command to delete all the rows with NaN value


>>>Test.dropna( )

Command to delete all the columns with NaN value


>>>Test.dropna(axis = 1)

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

You might also like