pandas DataFrame - truncate() | truncate rows or truncate table in pandas |
In this pandas tutorial we will discuss about:
-
truncate function pandas or truncate() function.
-
truncate table in pandas,
-
truncate rows in pandas,
Introduction
DataFrame in pandas is two dimensional data structure that store data in 2-dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can able to create this 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 pandas dataframe
In this example, we will create dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
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']
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: Our dataframe is created
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
Lets now learn about pandas truncate table.
truncate() or pandas truncate table
pandas truncate table or truncate() function is used to remove the rows and columns by selecting only particular rows. truncate means remove.
Syntax:
dataframe_input.truncate(before,after)
where,
-
dataframe_input is the input pandas dataframe
-
before is the index position where, data is truncated before that given position
-
after is the index position where, data is truncated after that position.
Note - Indexing starts with 0.
Example 1: truncate table in pandas example
In this truncate table in pandas example, we are going to truncate before 2nd row and after 3 rd row.
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]
})
#truncate before second row and after third row
print(data.truncate(before=1, after=2))
Output:
So, only will display second row and third row.
college_id college_name college_address Total Staff
1 c-021 vvit guntur 3422
2 c-002 RVR - JC guntur 5644
Example 2: truncate rows in pandas example
In this truncate rows in pandas example, we are going to truncate before 1 st row and after 3 rd row
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]
})
#truncate before first row and after second row
print(data.truncate(before=0, after=1))
Output:
So, only will display first row and second row.
college_id college_name college_address Total Staff
0 c-001 vignan university guntur 1200
1 c-021 vvit guntur 3422
This wraps up our session on truncate rows in pandas or truncate values in pandas or pandas truncate dataframe.
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 :
May 11,2023