pandas DataFrame - take() | pandas take function examples
In this pandas tutorial we will discuss about
-
take() or take function in pandas.
-
pandas take function example
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 learn about take function in pandas.
take() or take function in pandas
take() or take function in pandas is used to get the rows/columns from the dataframe.
It will get the data by using row/column indices.
Indexing starts with 0.
Syntax: take function in pandas
dataframe_input.take([indices],axis)
where,
1. dataframe_input is the pandas input dataframe
2. axis=0 specifies the row labels and axis=1 specifies the column labels.
3. indices are provided through the list.
Example 1: pandas take function example
In this pandas take function example, we will select the first, second and third columns 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]
})
#select first , second and third column
print(data.take([0, 1,2],axis=1))
Output:
From the above code, college_id, college_name, college_address were returned.
college_id college_name college_address
0 c-001 vignan university guntur
1 c-021 vvit guntur
2 c-002 RVR - JC guntur
3 c-004 Andhra University guntur
Example 2: pandas take function
In this pandas take function example, we will select the second and forth columns 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]
})
#select second and forth column
print(data.take([1,3],axis=1))
Output:
From the above code, college_name, Total Staff were returned.
college_name Total Staff
0 vignan university 1200
1 vvit 3422
2 RVR - JC 5644
3 Andhra University 670
Example 3: pandas take function
In this pandas take function example, we will select the first, second and third rows 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]
})
#select first , second and third row
print(data.take([0, 1,2],axis=0))
Output:
From the above code, row with indices - 0,1 and 2 were returned.
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
Example 4:
In this pandas take function example, we will select the second and forth rows 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]
})
#select second and forth row
print(data.take([1,3],axis=0))
Output:
From the above code, row with index - 1 and 3 were returned.
college_id college_name college_address Total Staff
1 c-021 vvit guntur 3422
3 c-004 Andhra University guntur 670
This wraps up our session on pandas take function.
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