Pandas
keyboard_arrow_down 655 guides
chevron_leftData Selection and Renaming
Method add_prefixMethod add_suffixMethod alignMethod at_timeMethod between_timeMethod dropMethod drop_duplicatesMethod duplicatedMethod equalsMethod filterMethod firstMethod getMethod headMethod idxmaxMethod idxminMethod lastMethod lookupMethod queryMethod reindexMethod renameMethod rename_axisMethod reset_indexMethod sampleMethod select_dtypesMethod set_axisMethod set_indexMethod tailMethod takeMethod truncate
check_circle
Mark as learned thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
Pandas DataFrame | get method
schedule Aug 12, 2023
Last updated local_offer
Tags Python●Pandas
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
Pandas's DataFrame.get(~)
is used to access columns of the DataFrame.
WARNING
The method get(~)
returns a reference to the columns, meaning that if you modify the returned value, the source DataFrame is also modified accordingly.
Parameters
1. key
| string
or list
of strings
The name of the column(s) you want to access.
Return Value
If
key
is a string, then a Series is returned.If
key
is a list of strings, then a DataFrame is returned.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":[1,2], "B":[3,4]})df
A B0 1 31 2 4
Accessing a single column
To access a single column, supply its column name:
df.get("A") # Returns a Series
0 11 2Name: A, dtype: int64
Accessing multiple columns
To access multiple columns, supply their column labels as a list
:
df.get(["A","B"]) # Returns a DataFrame
A B0 1 31 2 4
Risk of mutation
Consider the same df
as before:
df = pd.DataFrame({"A":[1,2],"B":[3,4]})df
A B0 1 31 2 4
We extract the column A
, and mutate one of its values:
df.get("A")[0] = 9df
A B0 9 31 2 4
As we can see our source DataFarme df
got mutated as well.
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
Official Pandas Documentation
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.get.html
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!