Pandas DataFrame.get
Pandas DataFrame.get
The DataFrame.get
method in pandas is used to retrieve a column or columns from a DataFrame by key (column name). If the key is not found, it returns a default value instead of raising an error. This method is useful for safely accessing columns in a DataFrame.
Syntax
The syntax for DataFrame.get
is:
DataFrame.get(key, default=None)
Here, DataFrame
refers to the pandas DataFrame from which the column is retrieved.
Parameters
Parameter | Description |
---|---|
key | The column name (key) to retrieve. Can also be a label or a list of labels for multiple columns. |
default | The default value to return if the specified key is not found. Defaults to None . |
Returns
A Series or DataFrame corresponding to the specified key, or the default
value if the key is not found.
Examples
Retrieving a Column by Key
Use get
to retrieve a column from a DataFrame.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000.5, 80000.0, 90000.0]
}
df = pd.DataFrame(data)
# Retrieve the 'Age' column
print("Age Column:")
age_column = df.get('Age')
print(age_column)
Output
Age Column:
0 25
1 30
2 35
Name: Age, dtype: int64
Handling Missing Keys with Default Values
Provide a default value to return when the specified key is not found.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000.5, 80000.0, 90000.0]
}
df = pd.DataFrame(data)
# Attempt to retrieve a non-existent column with a default value
print("Non-Existent Column:")
default_column = df.get('Department', default='Not Available')
print(default_column)
Output
Non-Existent Column:
Not Available
Retrieving Multiple Columns
Use a list of keys to retrieve multiple columns from a DataFrame.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000.5, 80000.0, 90000.0]
}
df = pd.DataFrame(data)
# Retrieve multiple columns
print("Selected Columns:")
selected_columns = df.get(['Name', 'Salary'])
print(selected_columns)
Output
Selected Columns:
Name Salary
0 Arjun 70000.5
1 Ram 80000.0
2 Priya 90000.0
Using get
with an Empty DataFrame
When using get
on an empty DataFrame, it will return the default value.
Python Program
import pandas as pd
# Create an empty DataFrame
df_empty = pd.DataFrame()
# Attempt to retrieve a column with a default value
print("Retrieving from an Empty DataFrame:")
default_empty = df_empty.get('Age', default='No Data')
print(default_empty)
Output
Retrieving from an Empty DataFrame:
No Data
Summary
In this tutorial, we explored the DataFrame.get
method in pandas. Key takeaways include:
- Using
get
to retrieve columns safely by key. - Providing default values to handle missing keys without raising errors.
- Retrieving multiple columns or handling empty DataFrames effectively.
The DataFrame.get
method is a flexible and safe way to access data in pandas DataFrames.