Python | Pandas dataframe.get()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas dataframe.get() function is used to get item from object for given key. The key could be one or more than one dataframe column. It returns default value if not found.

Syntax: DataFrame.get(key, default=None)

Parameters :
key : object

Returns : value : type of items contained in object

For link to CSV file Used in Code, click here

Example #1: Use get() function to extract a column out of the dataframe

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the dataframe 
df = pd.read_csv("nba.csv")
  
# Print the dataframe
df

chevron_right


Output :

Now apply the get() function. We are going to extract the “Salary” column from the dataframe.

filter_none

edit
close

play_arrow

link
brightness_4
code

# applying get() function 
df.get("Salary")

chevron_right


Output :

Notice, the output is not a dataframe but a pandas series object.
 
Example #2: Use get() function to extract multiple columns at a time in random order

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the dataframe 
df = pd.read_csv("nba.csv")
  
df.get(["Salary", "Team", "Name"])

chevron_right


Output :

A dataframe is returned as output. The ordering of the columns is not according to the actual dataframe but it follows the ordering that we provided to it in the function input.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.