chevron_left
Selecting Data Cookbook
Accessing a single value of a DataFrameAccessing columns of a DataFrame using column labelsAccessing columns of a DataFrame using integer indicesAccessing rows of a DataFrame using integer indicesAccessing rows of a DataFrame using row labelsAccessing the first n rowsAccessing the last n rowsAccessing values of a multi-index DataFrameAdding prefix to column labelsAdding suffix to column labelsConverting two columns into a dictionaryExcluding columns based on typeGetting earliest or latest date from DataFrameGetting every nth rowGetting indexes of rows matching conditionsGetting shortest and longest stringsSelecting a single column as a DataFrameSelecting columns of a DataFrame using regexSelecting last column of DataFrameSelecting rows based on a conditionExtracting values of a DataFrame as a Numpy arrayGetting a list of all the column labelsGetting all columns except oneGetting all duplicate rowsGetting all numeric columns of a DataFrameGetting all unique values of columnsGetting column label of max value in each rowGetting column label of minimum value in each rowGetting columns by data typeGetting columns using integer indexGetting first row value of a columnGetting index of Series where value is TrueGetting integer index of a column using its column labelGetting integer index of rows based on column valuesGetting multiple columnsGetting row with largest index valueGetting row with smallest index valueGetting rows based on multiple column valuesGetting rows except someGetting rows from a DataFrame based on column valuesGetting rows that are not in other DataFrameGetting rows using OR statementGetting rows where column values are of specific lengthGetting rows where value is between two valuesGetting rows where values do not contain substringGetting the column labels of a DataFrameGetting the first columnGetting the index of a DataFrameGetting the length of the longest string in a columnGetting the longest string in a columnGetting the row with the maximum column valueGetting the row with the minimum column valueGetting the shape of a DataFrameGetting number of columns of a DataFrameGetting the total number of rows of a DataFrameGetting the total number of values in a DataFrameMaking column labels all lowercaseMaking column labels all uppercaseRandomly select rows based on a conditionRandomly selecting n columns from a DataFrameRandomly selecting n rows from a DataFrameReassigning column valuesRetrieving DataFrame column values as a NumPy arraySelecting columns that do not begin with certain prefixSelecting columns with certain prefixSelecting n rows with the smallest values for a columnSelecting rows based on datesSelecting rows from a DataFrame whose column values are contained in a listSelecting rows from a DataFrame whose column values are NOT contained in a listSelecting rows from a DataFrame whose column values contain a substringSelecting top n rows with the largest values for a columnSplitting DataFrame based on column values
0
0
0
new
Accessing columns of a DataFrame using column labels in Pandas
Programming
chevron_rightPython
chevron_rightPandas
chevron_rightCookbooks
chevron_rightDataFrame Cookbooks
chevron_rightSelecting Data Cookbook
schedule Jul 1, 2022
Last updated Python●Pandas
Tags tocTable of Contents
expand_more To access specific columns of a DataFrame with their columns labels, directly use DataFrame[~]
or use the DataFrame.loc
property.
Example
Consider the following DataFrame:
df
A Ba 3 5b 4 6
Accessing a single column
To access a single column:
df["A"]
a 3b 4Name: A, dtype: int64
We could also use the loc
property, which is slightly more verbose:
a 3b 4Name: A, dtype: int64
Here, the :
before the comma indicates that we want to retrieve all rows. The "A"
after the comma then indicates that we just want to fetch column A
.
Accessing multiple columns
Consider the same df
as above:
df
A Ba 3 5b 4 6
To access multiple columns, pass in a list of column labels:
df[["A","B"]]
A Ba 3 5b 4 6
Using loc
property:
A Ba 3 5b 4 6
The only difference with the single-case is that here we pass in a list of column labels as opposed to a single string.
Related
Pandas DataFrame | loc property
Access and update values of the DataFrame using row and column labels.
Accessing columns of a DataFrame using integer indices in Pandas
To access columns of a DataFrame using integer indices in Pandas, use the DataFrame.iloc property.
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
0
0
Enjoy our search