Skip to content

Added example to Dataframe.get docs #43233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 27, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3991,6 +3991,38 @@ def get(self, key, default=None):
Returns
-------
value : same type as items contained in object

Examples
--------
>>> df = pd.DataFrame([[24.3, 75.7, 'high'],
... [31, 87.8, 'high'],
... [22, 71.6, 'medium'],
... [35, 95, 'medium']],
... columns=['temp_celsius', 'temp_fahrenheit',
... 'windspeed'],
... index=pd.date_range(start='2014-02-12',
... end='2014-02-15', freq='D'))

>>> df
temp_celsius temp_fahrenheit windspeed
2014-02-12 24.3 75.7 high
2014-02-13 31.0 87.8 high
2014-02-14 22.0 71.6 medium
2014-02-15 35.0 95.0 medium

>>> df1 = df.get(["temp_celsius","windspeed"])

>>> df1
temp_celsius windspeed
2014-02-12 24.3 high
2014-02-13 31.0 high
2014-02-14 22.0 medium
2014-02-15 35.0 medium
Copy link
Member

@MarcoGorelli MarcoGorelli Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to assign to a new variable, let's just print the first one

Suggested change
>>> df1 = df.get(["temp_celsius","windspeed"])
>>> df1
temp_celsius windspeed
2014-02-12 24.3 high
2014-02-13 31.0 high
2014-02-14 22.0 medium
2014-02-15 35.0 medium
>>> df.get(["temp_celsius", "windspeed"])
temp_celsius windspeed
2014-02-12 24.3 high
2014-02-13 31.0 high
2014-02-14 22.0 medium
2014-02-15 35.0 medium


>>> df2 = df.get(["temp_celsius","temp_kelvin"], default = "default_value")

>>> df2
'default_value'
Copy link
Member

@MarcoGorelli MarcoGorelli Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise, and dedent the result

Suggested change
>>> df2 = df.get(["temp_celsius","temp_kelvin"], default = "default_value")
>>> df2
'default_value'
>>> df.get(["temp_celsius", "temp_kelvin"], default = "default_value")
'default_value'

"""
try:
return self[key]
Expand Down